File tree Expand file tree Collapse file tree
longest-repeating-character-replacement Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Solution :
2+ def characterReplacement (self , s : str , k : int ) -> int :
3+ count = {}
4+
5+ # ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ๋ณ์ (์ต๋ ๊ธธ์ด)
6+ max_length = 0
7+
8+ # ์๋์ฐ ์ผ์ชฝ ํฌ์ธํฐ
9+ left = 0
10+
11+ # ํ์ฌ ์๋์ฐ ๋ด์์ ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋ฌธ์์ ๋น๋์
12+ max_frequency = 0
13+
14+ # right ํฌ์ธํฐ๋ฅผ 0๋ถํฐ ๋๊น์ง ์ด๋
15+ for right in range (len (s )):
16+ current_char = s [right ]
17+
18+ # ํ์ฌ ๋ฌธ์์ ์นด์ดํธ ์ฆ๊ฐ
19+ count [current_char ] = count .get (current_char , 0 ) + 1
20+
21+ # ํ์ฌ ์๋์ฐ ๋ด์ '์ต๋น ๋ฌธ์' ๊ฐ์ ๊ฐฑ์
22+ # ์๋ก ๋ค์ด์จ ๋ฌธ์๊ฐ ์ต๋น ๋ฌธ์๊ฐ ๋ ์๋ ์์ผ๋ฏ๋ก ๋น๊ต
23+ max_frequency = max (max_frequency , count [current_char ])
24+
25+ # ์๋์ฐ ํฌ๊ธฐ = (right - left + 1)
26+ # ๋๋จธ์ง ๋ฌธ์ ๊ฐ์ = ์๋์ฐ ํฌ๊ธฐ - ์ต๋น ๋ฌธ์ ๊ฐ์
27+ window_len = right - left + 1
28+ if (window_len - max_frequency ) > k :
29+ # ์ผ์ชฝ ๋ฌธ์๋ฅผ ์๋์ฐ์์ ์ ๊ฑฐ
30+ left_char = s [left ]
31+ count [left_char ] -= 1
32+ left += 1 # ์ผ์ชฝ ํฌ์ธํฐ ์ด๋
33+ max_length = max (max_length , right - left + 1 )
34+
35+ return max_length
You canโt perform that action at this time.
0 commit comments