forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
48 lines (36 loc) · 909 Bytes
/
main.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
/// Source : https://leetcode.com/problems/remove-letter-to-equalize-frequency/
/// Author : liuyubobobo
/// Time : 2022-10-02
#include <iostream>
#include <vector>
#include <map>
using namespace std;
/// Brute Force
/// Time Compelxity: O(|word| + 26^2)
/// Space Complexity: O(26)
class Solution {
public:
bool equalFrequency(string word) {
vector<int> f(26, 0);
for(char c: word) f[c - 'a'] ++;
for(int i = 0; i < 26; i ++)
if(f[i]){
f[i] --;
if(ok(f)) return true;
f[i] ++;
}
return false;
}
private:
bool ok(const vector<int>& f){
int t = -1;
for(int i = 0; i < 26 && t == -1; i ++)
if(f[i]) t = f[i];
for(int i = 0; i < 26; i ++)
if(f[i] && f[i] != t) return false;
return true;
}
};
int main() {
return 0;
}