forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-letter-to-equalize-frequency.cpp
62 lines (60 loc) · 1.47 KB
/
remove-letter-to-equalize-frequency.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
// Time: O(n)
// Space: O(1)
// freq table, edge cases
class Solution {
public:
bool equalFrequency(string word) {
vector<int> cnt(26);
for (const auto& c : word) {
++cnt[c - 'a'];
}
unordered_map<int, int> cnt2;
for (const auto& c : cnt) {
if (c) {
++cnt2[c];
}
}
if (size(cnt2) > 2) {
return false;
}
if (size(cnt2) == 1) {
return cbegin(cnt2)->first == 1 || cbegin(cnt2)->second == 1;
}
int a = cbegin(cnt2)->first;
int b = next(cbegin(cnt2))->first;
if (a > b) {
swap(a, b);
}
return (a == 1 && cnt2[a] == 1) || (a + 1 == b && cnt2[b] == 1);
}
};
// Time: O(26 * n)
// Space: O(1)
// brute force, freq table
class Solution2 {
public:
bool equalFrequency(string word) {
vector<int> cnt(26);
for (const auto& c : word) {
++cnt[c - 'a'];
}
for (const auto& c : word) {
--cnt[c - 'a'];
int i = 0;
for (int prev = 0; i < size(cnt); ++i) {
if (!cnt[i]) {
continue;
}
if (prev && prev != cnt[i]) {
break;
}
prev = cnt[i];
}
if (i == size(cnt)) {
return true;
}
++cnt[c - 'a'];
}
return false;
}
};