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+ """
2+ # Approach
3+ 1. ๊ตฌ๊ฐ์์ ์ ์ผ ๋ง์ ๋ฌธ์ ์ฐพ๋๋ค
4+ 2. ๋๋จธ์ง๋ฅผ ๋ฐ๊พผ๋ค
5+ 3. ๋ฐ๊พธ๋ ๊ฐ์๊ฐ k ์ดํ์ธ์ง ๋ณธ๋ค
6+ 4. ๊ฐ๋ฅํ๋ฉด ๊ตฌ๊ฐ ๊ธธ์ด๋ฅผ ํค์ด๋ค
7+
8+ # Complexity
9+ ๋ฌธ์์ด s์์ ์๋ก ๋ค๋ฅธ ๊ธ์์ ์ข
๋ฅ U, s์ ๊ธธ์ด N
10+ - Time complexity: O(N)
11+ - Space complexity: O(U) => ์์ด ๋๋ฌธ์๋ง ๋์ค๋ฏ๋ก ์ฌ์ค์ O(1)
12+ """
13+
14+ from collections import defaultdict
15+
16+
17+ class Solution :
18+ def characterReplacement (self , s : str , k : int ) -> int :
19+ count = defaultdict (int )
20+ left = 0
21+ max_freq = 0
22+ answer = 0
23+
24+ for right in range (len (s )):
25+ count [s [right ]] += 1
26+ max_freq = max (max_freq , count [s [right ]])
27+
28+ while (right - left + 1 ) - max_freq > k :
29+ count [s [left ]] -= 1
30+ left += 1
31+
32+ answer = max (answer , right - left + 1 )
33+
34+ return answer
You canโt perform that action at this time.
0 commit comments