Skip to content

Commit 447ae57

Browse files
committed
#244 longest-repeating-chracter-replacement solution
1 parent 82d6ace commit 447ae57

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
ํ’€์ด :
3+
๋ถ€๋ถ„๋ฌธ์ž์—ด ์ค‘์— ๊ฐ€์žฅ ๋งŽ์€ ๋นˆ๋„์˜ ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ k๊ฐœ ์ดํ•˜๋กœ ์žˆ์œผ๋ฉด
4+
๊ฐ™์€ ๋ฐ˜๋ณต๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค
5+
sliding window ๊ธฐ๋ฒ•์„ ํ†ตํ•ด ์ตœ๋‹ค๋นˆ๋„ ๋ฌธ์ž์™€ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ k๊ฐœ ์ดˆ๊ณผ์ด๋ฉด start๋ฅผ ์ด๋™์‹œํ‚จ๋‹ค
6+
7+
SC : O(N)
8+
start, endํฌ์ธํ„ฐ๋Š” ๋ฌธ์ž์—ด ๊ธธ์ด์— ๋น„๋ก€ํ•ด ๋ฐ˜๋ณตํ•ด์„œ ์ด๋™ํ•˜๋ฏ€๋กœ (max ์—ฐ์‚ฐ์€ O(26))
9+
10+
TC : O(1)
11+
counter ๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ตœ๋Œ€ ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜๋งŒํผ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•˜๋ฏ€๋กœ O(26)์œผ๋กœ ์ƒ์ˆ˜์ด๋‹ค๋‹ค
12+
"""
13+
14+
class Solution:
15+
def characterReplacement(self, s: str, k: int) -> int:
16+
start, end = 0, 0
17+
counter = {}
18+
max_len = 0
19+
while end < len(s) :
20+
counter[s[end]] = counter.get(s[end], 0) + 1
21+
while end - start + 1 - max(counter.values()) > k :
22+
counter[s[start]] -= 1
23+
start += 1
24+
max_len = max(max_len, end - start + 1)
25+
end += 1
26+
return max_len

0 commit comments

Comments
ย (0)