Skip to content

Commit d679ed8

Browse files
committed
longest repeating character replacement solution
1 parent d3aec9c commit d679ed8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function characterReplacement(s: string, k: number): number {
2+
let longestLength = 0;
3+
let windowStart = 0;
4+
5+
const countMap = new Map<string, number>();
6+
let maxCount = 0;
7+
8+
for (let windowEnd = 0; windowEnd < s.length; windowEnd++) {
9+
const currentCh = s[windowEnd];
10+
countMap.set(currentCh, (countMap.get(currentCh) || 0) + 1);
11+
12+
maxCount = Math.max(maxCount, countMap.get(currentCh) || 0);
13+
if (windowEnd - windowStart + 1 - maxCount > k) {
14+
countMap.set(s[windowStart], (countMap.get(s[windowStart]) || 0) - 1);
15+
16+
windowStart++;
17+
}
18+
longestLength = Math.max(longestLength, windowEnd - windowStart + 1);
19+
}
20+
21+
return longestLength;
22+
}

0 commit comments

Comments
 (0)