Skip to content

Commit 859f8e4

Browse files
authored
Create unique-substrings-with-equal-digit-frequency.cpp
1 parent 28219ce commit 859f8e4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: O(n^2)
2+
// Space: O(n^2)
3+
4+
// rolling hash
5+
class Solution {
6+
public:
7+
int equalDigitFrequency(string s) {
8+
static const int MOD = 1e9 + 7;
9+
static const int D = 27;
10+
11+
unordered_set<int> lookup;
12+
for (int i = 0; i < size(s); ++i) {
13+
unordered_map<int, int> cnt;
14+
int64_t h = 0;
15+
int max_cnt = 0;
16+
for (int j = i; j < size(s); ++j) {
17+
const int d = s[j] - '0' + 1;
18+
h = (h * D + d) % MOD;
19+
++cnt[d];
20+
max_cnt = max(max_cnt, cnt[d]);
21+
if (size(cnt) * max_cnt == j - i + 1) {
22+
lookup.emplace(h);
23+
}
24+
}
25+
}
26+
return size(lookup);
27+
}
28+
};

0 commit comments

Comments
 (0)