File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +33
-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+ # Time Complexity: O(n)
4+ # Space Complexity: O(1)
5+ n = len (s )
6+ if n == 0 :
7+ return 0
8+
9+ left = 0
10+ max_len = 0
11+ char_counts = {}
12+ max_freq_count = 0
13+
14+ for right in range (n ):
15+ right_char = s [right ]
16+ char_counts [right_char ] = char_counts .get (right_char , 0 ) + 1
17+ max_freq_count = max (max_freq_count , char_counts [right_char ])
18+
19+ current_window_length = right - left + 1
20+ # ๋ฐ๊ฟ์ผ ํ๋ ๋ฌธ์ ์ = ์๋์ฐ ๊ธธ์ด - ๊ฐ์ฅ ๋ง์ ๋ฌธ์์ ๋น๋์
21+ changes_needed = current_window_length - max_freq_count
22+
23+ # ๋ง์ฝ ๋ฐ๊ฟ์ผ ํ๋ ๋ฌธ์ ์๊ฐ k๋ณด๋ค ํฌ๋ฉด ์๋์ฐ๋ฅผ ์ค์ฌ์ผ ํจ
24+ if changes_needed > k :
25+ left_char = s [left ]
26+ char_counts [left_char ] -= 1
27+ if char_counts [left_char ] == 0 :
28+ del char_counts [left_char ]
29+ left += 1
30+
31+ max_len = max (max_len , right - left + 1 )
32+
33+ return max_len
You canโt perform that action at this time.
0 commit comments