File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def characterReplacement (self , s : str , k : int ) -> int :
3
+
4
+ # ์ฌ๋ผ์ด๋ฉ ์๋์ฐ(ํฌํฌ์ธํฐ)
5
+ count = {} # ๋ฌธ์ ๋น๋์
6
+ left = 0 # ์ผ์ชฝ ํฌ์ธํฐ
7
+ max_count = 0 # ํ์ฌ ์๋์ฐ์์ ์ต๋ ๋ฌธ์ ๋น๋์
8
+ answer = 0 # ๊ฐ์ฅ ๊ธด ๋์ผ ๋ฌธ์ ๋ถ๋ถ ๋ฌธ์์ด์ ์ต๋ ๊ธธ์ด
9
+
10
+ # ์ค๋ฅธ์ชฝ ํฌ์ธํฐ๋ฅผ ํ ์นธ์ฉ ๋๋ ค๊ฐ๋ฉฐ ์๋์ฐ ํ์ฅ
11
+ for right in range (len (s )):
12
+ count [s [right ]] = count .get (s [right ],0 ) + 1 # ํ์ฌ ๋ฌธ์ ์นด์ดํธ ์ฆ๊ฐ
13
+ max_count = max (max_count , count [s [right ]]) # ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋ฌธ์ ์ ๊ฐฑ์
14
+
15
+ # ์๋์ฐ๊ธธ์ด(right-left+1) - ๊ฐ์ฅ์์ฃผ๋์จ๋ฌธ์ ๋น๋์ > k : ํ์ฌ ์๋์ฐ์์ ๋ฐ๊ฟ์ผํ๋ ๋ฌธ์๊ฐ ๋ ๋ง์ผ๋ฉด ์ผ์ชฝ ํฌ์ธํฐ ์ด๋
16
+ if (right - left + 1 ) - max_count > k :
17
+ count [s [left ]] -= 1 # ์ผ์ชฝ ๋ฌธ์ ์ ๊ฑฐ
18
+ left += 1 # ์ผ์ชฝ ํฌ์ธํฐ ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ ์ด๋
19
+
20
+ answer = max (answer , right - left + 1 )
21
+
22
+ return answer
You canโt perform that action at this time.
0 commit comments