-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_2168_equalDigitFrequency.cc
More file actions
53 lines (51 loc) · 1.12 KB
/
Copy pathProblem_2168_equalDigitFrequency.cc
File metadata and controls
53 lines (51 loc) · 1.12 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution
{
public:
int equalDigitFrequency(string s)
{
long base = 499;
int n = s.length();
unordered_set<long> set;
vector<int> cnt(10);
for (int left = 0; left < n; left++)
{
long hashCode = 0;
int curVal = 0;
int maxCnt = 0;
int maxCntkinds = 0;
int allKinds = 0;
for (int right = left; right < n; right++)
{
curVal = s[right] - '0';
hashCode = hashCode * base + curVal + 1;
cnt[curVal]++;
if (cnt[curVal] == 1)
{
// 新种类字符
allKinds++;
}
if (cnt[curVal] > maxCnt)
{
// 最大数量的字符
maxCnt = cnt[curVal];
maxCntkinds = 1;
}
else if (cnt[curVal] == maxCnt)
{
// 新的字符串达到最大数量
maxCntkinds++;
}
if (maxCntkinds == allKinds)
{
// 所有的字符都达到最大数量
set.emplace(hashCode);
}
}
}
return set.size();
}
};