-
Notifications
You must be signed in to change notification settings - Fork 0
/
395.hpp
52 lines (41 loc) · 1.1 KB
/
395.hpp
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
//
// Created by bai on 17-6-25.
//
#ifndef LEETCODE_395_HPP
#define LEETCODE_395_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <stack>
using namespace std;
class Solution {
public:
int longestSubstring(string s, int k) {
unordered_map<char, int> mp;
for_each(s.begin(), s.end(), [&mp](char c) {
mp[c]++;
});
auto f = find_if(mp.begin(), mp.end(), [k](pair<const char, int> &p) {
return p.second < k;
});
if (f == mp.end()) {
return (int) s.size();
}
auto c = (*f).first;
int mlen = 0;
unsigned long split = 0;
auto p = s.find(c, split);
while (p != string::npos) {
mlen = max(mlen, longestSubstring(s.substr(split, p), k));
split = p + 1;
while (p <= split && p != string::npos) {
split = p + 1;
p = s.find(c, split);
}
}
mlen = max(mlen, longestSubstring(s.substr(split), k));
return mlen;
}
};
#endif //LEETCODE_395_HPP