leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2423.cpp (685B)
0 class Solution {
1 int count[27] = {};
2 bool check(int crnt, int goal) {
3 for (int j = 1; j <= 26; j++) {
4 if (crnt == j || !count[j]) continue;
5 if (goal == -1)
6 goal = count[j];
7 else if (count[j] != goal)
8 return false;
9 }
10 return true;
11 }
13 public:
14 bool equalFrequency(const string &word) {
15 for (const char c : word)
16 count[c & 0x1F]++;
18 for (int i = 1; i <= 26; i++) {
19 if (!count[i]) continue;
20 const int goal = count[i] > 1 ? count[i] - 1 : -1;
21 if (check(i, goal)) return true;
22 }
24 return false;
25 }
26 };