File tree Expand file tree Collapse file tree 2 files changed +45
-8
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 2 files changed +45
-8
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Constraints:
3
+ - 1 <= s.length <= 10^5
4
+ - s consists of only uppercase English letters.
5
+ - 0 <= k <= s.length
6
+
7
+ Time Complexity: O(n)
8
+ - ์ฌ๊ธฐ์ n์ ๋ฌธ์์ด์ ๊ธธ์ด
9
+
10
+ Space Complexity: O(1)
11
+ - ์ถ๊ฐ ๋ณ์(left, right, max_length ๋ฑ)๋ ์์ ๊ฐ
12
+
13
+ ํ์ด๋ฐฉ๋ฒ:
14
+ 1. Sliding Window๋ก ๊ตฌ๊ฐ์ ๊ด๋ฆฌ
15
+ - right ํฌ์ธํฐ๋ก ๊ตฌ๊ฐ์ ๋๋ฆฌ๋ค๊ฐ
16
+ - ๋ณ๊ฒฝํด์ผํ๋ ๋ฌธ์ ์๊ฐ k๋ฅผ ์ด๊ณผํ๋ฉด left ํฌ์ธํฐ๋ก ๊ตฌ๊ฐ์ ์ค์
17
+
18
+ 2. ๊ฐ ๊ตฌ๊ฐ์์:
19
+ - ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋ฌธ์๋ก ๋๋จธ์ง๋ฅผ ๋ณ๊ฒฝ
20
+ - (๊ตฌ๊ฐ ๊ธธ์ด - ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋ฌธ์ ์)๊ฐ k ์ดํ์ฌ์ผ ํจ
21
+ """
22
+ class Solution :
23
+ def characterReplacement (self , s : str , k : int ) -> int :
24
+ counter = {}
25
+ left = 0
26
+ max_length = 0
27
+
28
+ for right in range (len (s )):
29
+ counter [s [right ]] = counter .get (s [right ], 0 ) + 1
30
+
31
+ curr_length = right - left + 1
32
+
33
+ if curr_length - max (counter .values ()) > k :
34
+ counter [s [left ]] -= 1
35
+ left += 1
36
+
37
+ max_length = max (max_length , right - left + 1 )
38
+
39
+ return max_length
Original file line number Diff line number Diff line change 10
10
- count ๋ณ์๋ง ์ฌ์ฉํ๋ฏ๋ก ์์ ๊ณต๊ฐ ๋ณต์ก๋
11
11
"""
12
12
class Solution :
13
- def hammingWeight (self , n : int ) -> int :
14
- count = 0
15
- while n :
16
- count += n & 1
17
- n >>= 1
18
- return count
19
-
20
-
13
+ def hammingWeight (self , n : int ) -> int :
14
+ count = 0
15
+ while n :
16
+ count += n & 1 # ํ์ฌ ๋ง์ง๋ง ๋นํธ๊ฐ 1์ธ์ง ํ์ธ
17
+ n >>= 1 # ๋ค์ ๋นํธ ๊ฒ์ฌ๋ฅผ ์ํด ์ค๋ฅธ์ชฝ ์ํํธ
18
+ return count
You canโt perform that action at this time.
0 commit comments