-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check if frequencies can be equal.cpp
67 lines (62 loc) · 1.16 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
59
60
61
62
63
64
65
66
67
// Time complexity - O(N)
// Space complexity- O(2)
typedef pair<int,int> pr;
class Solution{
public:
bool sameFreq(string s)
{
unordered_map<char,int> mp;
for(auto it:s)
mp[it]++;
unordered_map<int,int> mp2;
for(auto it:mp)
{
mp2[it.second]++;
if(mp2.size()>2)
return 0;
}
if(mp2.size()<=1)
return 1;
pr a={0,0};
pr b={0,0};
for(auto it:mp2)
{
if(a.first==0)
{
a.first=it.first;
a.second=it.second;
}
else
{
b.first=it.first;
b.second=it.second;
}
}
if(a.second!=1 and b.second!=1)
return 0;
else if(a.first==1 or b.first==1)
return 1;
else
{
if(a.second>1)
{
if(b.first-a.first<0)
return 0;
else if(b.first-a.first<=1)
return 1;
else
return 0;
}
else
{
if(a.first-b.first<0)
return 0;
else if(a.first-b.first<=1)
return 1;
else
return 0;
}
}
return 1;
}
};