-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
43 lines (36 loc) · 821 Bytes
/
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
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var characterReplacement = function(s, k) {
const len = s.length;
let max = 0;
let strdict = {};
let left = 0, right = 0;
for ( ; right < len; right++) {
const e = s[right];
if(strdict[e] !== undefined) {
strdict[e] += 1;
} else {
strdict[e] = 1;
}
let curMaxStr = '';
Object.keys(strdict).map(ele => {
if (strdict[ele] > (strdict[curMaxStr] || 0)) {
curMaxStr = ele;
}
});
while(right-left+1-strdict[curMaxStr] > k) {
strdict[s[left]] -= 1;
left += 1;
Object.keys(strdict).map(ele => {
if (strdict[ele] > strdict[curMaxStr]) {
curMaxStr = ele;
}
});
}
max = Math.max(max, right-left+1);
}
return max;
};