-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
51 lines (45 loc) · 1.09 KB
/
res.js
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
/**
* 超时 - 分治方法
* @param {string} s
* @param {number} k
* @return {number}
*/
var longestSubstring = function(s, k) {
const len = s.length;
if (len<k) return 0;
const count = (start, end) => {
if (end-start+1 < k) return 0;
const strdict = {};
let numOfLessThanK = 0;
for (let i = start; i <= end; i++) {
const e = s[i];
if (strdict[e] === undefined) {
strdict[e] = 1;
numOfLessThanK += 1;
} else {
strdict[e] += 1;
if (strdict[e] === k) {
numOfLessThanK -= 1;
}
};
}
if (!numOfLessThanK) return end-start+1;
while(end-start+1 < k && strdict[start] < k) {
start++;
}
while(end-start+1 < k && strdict[end] < k) {
end--;
}
if (end-start+1 < k) return 0;
for (let i = start; i <= end; i++) {
const e = s[i];
if (strdict[e] < k) {
let next = i+1;
while(strdict[next] < k && next < end) next++;
return Math.max(count(start, i-1),count(next, end));
}
}
return end-start+1;
}
return count(0, len-1);
};