-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_0395_longestSubstring.cc
110 lines (104 loc) · 2.27 KB
/
Problem_0395_longestSubstring.cc
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 暴力枚举
int longestSubstring1(string s, int k)
{
int N = s.length();
int ans = 0;
for (int i = 0; i < N; i++)
{
vector<int> cnt(256);
int collect = 0;
int satisfy = 0;
for (int j = i; j < N; j++)
{
if (cnt[s[j]] == 0)
{
// 多少个不同的字符
collect++;
}
if (cnt[s[j]] == k - 1)
{
// 多少个字符数满足 >= k 的字符
satisfy++;
}
cnt[s[j]]++;
if (collect == satisfy)
{
ans = std::max(ans, j - i + 1);
}
}
}
return ans;
}
int longestSubstring2(string s, int k)
{
int N = s.length();
int max = 0;
for (int require = 1; require <= 26; require++)
{
// a ~ z 出现的次数
// cnt[0 1 2] a b c
vector<int> cnt(26);
// 窗口收集了集中字符
int collect = 0;
// 窗口出现次数 >=k 次的字符
int satisfy = 0;
int R = -1;
for (int L = 0; L < N; L++)
{
//[L ... R] R+1
// 循环退出条件:新字符会打破 collext == require 平衡条件
// 表示收集的字符已达到上限
while (R + 1 < N && !(collect == require && cnt[s[R + 1] - 'a'] == 0))
{
R++;
if (cnt[s[R] - 'a'] == 0)
{
collect++;
}
if (cnt[s[R] - 'a'] == k - 1)
{
satisfy++;
}
cnt[s[R] - 'a']++;
}
// [L ... R]
if (satisfy == require)
{
max = std::max(max, R - L + 1);
}
// 为 L++ 做准备
if (cnt[s[L] - 'a'] == 1)
{
collect--;
}
if (cnt[s[L] - 'a'] == k)
{
satisfy--;
}
cnt[s[L] - 'a']--;
}
}
return max;
}
};
void testLongestSubstring()
{
Solution s;
EXPECT_EQ_INT(3, s.longestSubstring1("aaabb", 3));
EXPECT_EQ_INT(3, s.longestSubstring1("aaabb", 3));
EXPECT_EQ_INT(5, s.longestSubstring2("ababbc", 2));
EXPECT_EQ_INT(5, s.longestSubstring2("ababbc", 2));
EXPECT_SUMMARY;
}
int main()
{
testLongestSubstring();
return 0;
}