Skip to content

Commit 1e9a6bb

Browse files
committed
longest-repeating-character-replacement solution
1 parent e692ca7 commit 1e9a6bb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
*
3+
* @param {string} s
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
var characterReplacement = function (s, k) {
8+
let start = 0;
9+
let maxLength = 0;
10+
let maxCharCount = 0;
11+
let count = {};
12+
13+
for (let end = 0; end < s.length; end++) {
14+
const char = s[end];
15+
count[char] = (count[char] || 0) + 1;
16+
17+
// ํ˜„์žฌ ์œˆ๋„์šฐ ์•ˆ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋‚˜์˜จ ๋ฌธ์ž ์ˆ˜ ๊ฐฑ์‹ 
18+
maxCharCount = Math.max(maxCharCount, count[char]);
19+
20+
// ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š” ๋ฌธ์ž ์ˆ˜๊ฐ€ k๋ณด๋‹ค ๋งŽ์œผ๋ฉด โ†’ ์™ผ์ชฝ ํฌ์ธํ„ฐ ์ด๋™
21+
if (end - start + 1 - maxCharCount > k) {
22+
count[s[start]]--;
23+
start++;
24+
}
25+
26+
// ํ˜„์žฌ ์œˆ๋„์šฐ ๊ธธ์ด๋กœ ์ตœ๋Œ€ ๊ธธ์ด ๊ฐฑ์‹ 
27+
maxLength = Math.max(maxLength, end - start + 1);
28+
}
29+
30+
return maxLength;
31+
};

0 commit comments

Comments
ย (0)