Skip to content

Commit 571f4f2

Browse files
committed
longest-repeating-character-replacement solved
1 parent c88abf9 commit 571f4f2

File tree

1 file changed

+25
-0
lines changed
  • longest-repeating-character-replacement

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var characterReplacement = function(s, k) {
7+
const count = {};
8+
let res = 0;
9+
let l = 0;
10+
let maxf = 0;
11+
12+
for (let r = 0; r < s.length; r++) {
13+
count[s[r]] = (count[s[r]] || 0) + 1;
14+
maxf = Math.max(maxf, count[s[r]]);
15+
16+
while ((r - l + 1) - maxf > k) {
17+
count[s[l]] -= 1;
18+
l += 1;
19+
}
20+
21+
res = Math.max(res, r - l + 1);
22+
}
23+
24+
return res;
25+
};

0 commit comments

Comments
 (0)