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; } }
|