-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check if frequencies can be equal.cpp
58 lines (51 loc) · 1.25 KB
/
Check if frequencies can be equal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
bool sameFreq(string s)
{
// code here
vector<int> cnt(26, 0);
for(int i=0; i<s.length(); i++){
cnt[s[i]-'a']++;
}
vector<pair<int, int>> freq(3, {0, 0});
int k = 0;
sort(cnt.begin(), cnt.end());
for(int i=0; i<26; i++){
if(cnt[i]==0) continue;
else {
if(freq[k].first == 0){
freq[k].first = cnt[i];
freq[k].second = 1;
} else if(freq[k].first == cnt[i]){
freq[k].second++;
} else {
k++;
if(k==2) return false;
freq[k].first = cnt[i];
freq[k].second = 1;
}
}
}
if(k==0 || freq[0].first == 1 && freq[0].second == 1) return true;
if(freq[1].first-freq[0].first == 1 && freq[1].second == 1) return true;
return false;
}
};
//{ Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
Solution ob;
cout<<ob.sameFreq(s)<<endl;
}
}
// } Driver Code Ends