2423.cpp (685B)
1 class Solution { 2 int count[27] = {}; 3 bool check(int crnt, int goal) { 4 for (int j = 1; j <= 26; j++) { 5 if (crnt == j || !count[j]) continue; 6 if (goal == -1) 7 goal = count[j]; 8 else if (count[j] != goal) 9 return false; 10 } 11 return true; 12 } 13 14 public: 15 bool equalFrequency(const string &word) { 16 for (const char c : word) 17 count[c & 0x1F]++; 18 19 for (int i = 1; i <= 26; i++) { 20 if (!count[i]) continue; 21 const int goal = count[i] > 1 ? count[i] - 1 : -1; 22 if (check(i, goal)) return true; 23 } 24 25 return false; 26 } 27 };