Skip to content

Commit 2a28adf

Browse files
authored
Merge pull request #1895 from hi-rachel/main
[hi-rachel] WEEK 08 solutions
2 parents db1cd17 + d087c80 commit 2a28adf

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

โ€Žlongest-repeating-character-replacement/hi-rachel.pyโ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,25 @@ def characterReplacement(self, s: str, k: int) -> int:
4444
max_len = max(max_len, right - left + 1)
4545

4646
return max_len
47+
48+
49+
from collections import Counter
50+
51+
class Solution:
52+
def characterReplacement(self, s: str, k: int) -> int:
53+
count = Counter()
54+
l = 0
55+
max_freq = 0
56+
max_length = 0
57+
58+
for r, ch in enumerate(s):
59+
count[ch] += 1
60+
max_freq = max(max_freq, count[ch])
61+
62+
if (r - l + 1) - max_freq > k:
63+
count[s[l]] -= 1
64+
l += 1
65+
66+
max_length = max(max_length, r - l + 1)
67+
68+
return max_length

โ€Žpalindromic-substrings/hi-rachel.pyโ€Ž

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,37 @@ def countSubstrings(self, s: str) -> int:
5555
cnt += 1
5656

5757
return cnt
58+
59+
60+
# 25/09/08 ๋ณต์Šต
61+
62+
"""
63+
โ€œ๋ชจ๋“  ํšŒ๋ฌธ์€ ์ค‘์‹ฌ์—์„œ ์ขŒ์šฐ ๋Œ€์นญ์ด๋‹คโ€
64+
โ†’ ๋”ฐ๋ผ์„œ ๋ชจ๋“  ์ค‘์‹ฌ์ ์—์„œ ์ขŒ์šฐ๋กœ ํ™•์žฅํ•˜๋ฉด์„œ ํšŒ๋ฌธ์ธ์ง€ ํ™•์ธํ•˜๋ฉด
65+
โ†’ ๋ชจ๋“  ํšŒ๋ฌธ substring์„ ํƒ์ƒ‰ ๊ฐ€๋Šฅ!
66+
67+
- ์ค‘์‹ฌ์ ์˜ ๊ฐœ์ˆ˜๋Š” ์ด 2n - 1๊ฐœ:
68+
- ํ™€์ˆ˜ ๊ธธ์ด: center = 0 ~ n-1 (expand(i, i))
69+
- ์ง์ˆ˜ ๊ธธ์ด: center = 0 ~ n-1 (expand(i, i+1))
70+
71+
TC: O(N^2)
72+
SC: O(1)
73+
"""
74+
75+
class Solution:
76+
def countSubstrings(self, s: str) -> int:
77+
count = 0
78+
for center in range(len(s)):
79+
count += self.expand(s, center, center)
80+
81+
count += self.expand(s, center, center + 1)
82+
return count
83+
84+
def expand(self, s, left, right):
85+
count = 0
86+
# ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๊ณ , palindrome์ด๋ฉด ํ™•์žฅ
87+
while left >= 0 and right < len(s) and s[left] == s[right]:
88+
count += 1
89+
left -= 1
90+
right += 1
91+
return count

0 commit comments

Comments
ย (0)