forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest Substring With AtLeast K Repeating Characters.cpp
More file actions
58 lines (52 loc) · 1.32 KB
/
Longest Substring With AtLeast K Repeating Characters.cpp
File metadata and controls
58 lines (52 loc) · 1.32 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
54
55
56
57
58
// URL : https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
const int NO_OF_CHARS = 256;
class Solution {
public:
int longestSubstring(string s, int k) {
if (s.size() == 0 || s.size() < k) return 0;
if (k == 1) return s.size();
int count[NO_OF_CHARS];
memset(count , 0, sizeof(count));
for (char ch : s) {
count[ch]++;
}
int i = 0;
for ( i = 0; i < NO_OF_CHARS; i++) {
if (count[i] != 0 && count[i] < k) break;
}
if ( i >= NO_OF_CHARS ) return s.size();
char least = 0;
for (int c = 0; c < NO_OF_CHARS; c++) {
if (count[c] == 0) continue;
if (least == 0) {
least = c;
} else if ( count[c] < count[least]) {
least = c;
}
}
vector<string> subs;
split(s, least, subs);
int res = 0;
for (string str : subs) {
res = max(res, longestSubstring(str, k));
}
return res;
return 0;
}
private:
inline int max(int x, int y) { return x > y ? x : y; }
inline void split(const string &s, char delim, vector<string> &elems) {
stringstream ss;
ss.str(s);
string item;
while (getline(ss, item, delim)) {
cout << item << endl;
elems.push_back(item);
}
}
inline vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
};