-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon k string find k-1 character
49 lines (46 loc) · 1.19 KB
/
amazon k string find k-1 character
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
vector<string> findK_1(string s, int k) {
unordered_map<char, int> charMap;
int dup = 0;
int start = 0;
unordered_set<string> strSet;
vector<string> res;
for (int i = 0; i < s.size(); i++) {
charMap[s[i]]++;
if (charMap[s[i]] == 2) {
dup++;
}
if (charMap[s[i]] == 3) {
while (start <= i) {
charMap[start]--;
if (charMap[start] == 2) {
start++;
break;
}
start++;
}
}
if (dup == 2) {
while (start <= i) {
charMap[start]--;
if (charMap[start] == 1) {
start++;
dup--;
break;
}
start++;
}
}
if (i - start + 1 == k) {
if (dup == 1) {
strSet.insert(s.substr(start, k));
}
charMap[start]--;
if (charMap[start] == 1) {
dup--;
}
start++;
}
}
res = new vector<string>(strSet.begin(), strSet.end());
return res;
}