File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Solution:
3
+ 1) sliding window ํ์ฉ
4
+ 2) window ์ ๋ค์ด๊ฐ๋ ๊ธ์๋ค์ counts ๋ผ๋ hash map ์ผ๋ก ๊ฐฏ์๋ฅผ ์ธ์ด์ค๋ค.
5
+ 3) ์ํ๋ฅผ ํ๋ฉด์ window size ๊ฐ counts์์ ๊ฐ์ฅ ํฐ์ซ์ + k ๋ณด๋ค ํฌ๋ฉด invalid window ์ด๊ธฐ์ left ๊ธ์ ๊ฐฏ์๋ฅผ ๋นผ๊ณ ํฌ์ธํฐ๋ฅผ ์ด๋ํด์ค๋ค.
6
+ 4) max_count ๋ ๊ธฐ์กด max ๊ฐ๊ณผ ํ์ฌ window ์ฌ์ด์ฆ ์ค ํฐ ์ซ์๋ฅผ ์
ํด์ค๋ค.
7
+
8
+ Time: O(n) = O(26n)
9
+ Space: O(1) = O(26)
10
+ """
11
+
12
+
13
+ class Solution :
14
+ def characterReplacement (self , s : str , k : int ) -> int :
15
+ counts = defaultdict (int )
16
+ max_count = 0
17
+
18
+ l , r = 0 , 0
19
+ while l <= r and r < len (s ):
20
+ counts [s [r ]] += 1
21
+ window_size = r - l + 1
22
+
23
+ if window_size > max (counts .values ()) + k :
24
+ counts [s [l ]] -= 1
25
+ l += 1
26
+
27
+ window_size = r - l + 1
28
+ max_count = max (window_size , max_count )
29
+ r += 1
30
+
31
+ return max_count
You canโt perform that action at this time.
0 commit comments