Skip to content

Commit a32ed9d

Browse files
committed
longest-repeating-character-replacement solution
1 parent 0861d06 commit a32ed9d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var characterReplacement = function(s, k) {
7+
let start = 0;
8+
let maxFreq = 0;
9+
let result = 0;
10+
const map = new Map();
11+
12+
for (let end = 0; end < s.length; end++) {
13+
const endChar = s[end];
14+
map.set(endChar, 1 + (map.get(endChar) || 0));
15+
16+
maxFreq = Math.max(maxFreq, map.get(endChar));
17+
18+
if (end - start + 1 - maxFreq > k) {
19+
const startChar = s[start];
20+
map.set(startChar, map.get(startChar) - 1);
21+
start++;
22+
23+
}
24+
25+
result = Math.max(result, end - start + 1);
26+
}
27+
28+
return result;
29+
};
30+
31+
// 시간복잡도: O(n)
32+
// 공간복잡도: O(1)

0 commit comments

Comments
 (0)