LC.P2423[删除字符使频率相同]

方法一:暴力+哈希表

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
class Solution {
public boolean equalFrequency(String word) {
int n = word.length();
for (int i = 0; i < n; ++i) {
Map<Character, Integer> map = new HashMap<>();
for (int j = 0; j < n; ++j) {
if (j != i) {
map.merge(word.charAt(j), 1, Integer::sum);
}
}
if (check(map)) return true;
}
return false;
}

/**
* 检查剩余字符出现的次数是否相同
*/
private boolean check(Map<Character, Integer> map) {
int x = map.entrySet().iterator().next().getValue();
for (int v : map.values()) {
if (x != v) return false;
}
return true;
}
}