-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hi-rachel] WEEK 08 solutions #1895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,25 @@ def characterReplacement(self, s: str, k: int) -> int: | |
max_len = max(max_len, right - left + 1) | ||
|
||
return max_len | ||
|
||
|
||
from collections import Counter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Counter 대신 dict를 써도 좋을 것 같아요 |
||
|
||
class Solution: | ||
def characterReplacement(self, s: str, k: int) -> int: | ||
count = Counter() | ||
l = 0 | ||
max_freq = 0 | ||
max_length = 0 | ||
|
||
for r, ch in enumerate(s): | ||
count[ch] += 1 | ||
max_freq = max(max_freq, count[ch]) | ||
|
||
if (r - l + 1) - max_freq > k: | ||
count[s[l]] -= 1 | ||
l += 1 | ||
|
||
max_length = max(max_length, r - l + 1) | ||
|
||
return max_length |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모든 중심점에서 좌우로 확장하면서 하는 방법도 있다는 것을 배울 수 있었습니다. 저는 DP로 풀었는데 @hi-rachel 님이 풀이한 방법으로도 풀이를 해봐야겠네요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hyer0705 저는 DP로도 풀어봐야겠네요. 감사합니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,38 @@ def countSubstrings(self, s: str) -> int: | |
cnt += 1 | ||
|
||
return cnt | ||
|
||
|
||
# 25/09/08 복습 | ||
|
||
""" | ||
“모든 회문은 중심에서 좌우 대칭이다” | ||
→ 따라서 모든 중심점에서 좌우로 확장하면서 회문인지 확인하면 | ||
→ 모든 회문 substring을 탐색 가능! | ||
- 중심점의 개수는 총 2n - 1개: | ||
- 홀수 길이: center = 0 ~ n-1 (expand(i, i)) | ||
- 짝수 길이: center = 0 ~ n-1 (expand(i, i+1)) | ||
TC: O(N^2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 시간 복잡도는 O(N^2)이 맞지만, 긴 문자열 입력에서는 최적화(예: DP 방식)도 고려 가능 할 것 같습니다 |
||
SC: O(1) | ||
""" | ||
|
||
class Solution: | ||
def countSubstrings(self, s: str) -> int: | ||
count = 0 | ||
for center in range(len(s)): | ||
print(center) | ||
|
||
count += self.expand(s, center, center) | ||
|
||
count += self.expand(s, center, center + 1) | ||
return count | ||
|
||
def expand(self, s, left, right): | ||
count = 0 | ||
# 범위를 벗어나지 않고, palindrome이면 확장 | ||
while left >= 0 and right < len(s) and s[left] == s[right]: | ||
count += 1 | ||
left -= 1 | ||
right += 1 | ||
return count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
슬라이딩 윈도우 알고리즘을 사용하신 걸까요? 저도 비슷하게 풀이해서 코드를 이해하기 쉬웠습니다! l이나 r 이라는 변수명이 짧지만 가독성을 해치진 않네요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hyer0705 네 맞습니다!