Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions longest-repeating-character-replacement/hi-rachel.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

슬라이딩 윈도우 알고리즘을 사용하신 걸까요? 저도 비슷하게 풀이해서 코드를 이해하기 쉬웠습니다! l이나 r 이라는 변수명이 짧지만 가독성을 해치진 않네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyer0705 네 맞습니다!

Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
35 changes: 35 additions & 0 deletions palindromic-substrings/hi-rachel.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 중심점에서 좌우로 확장하면서 하는 방법도 있다는 것을 배울 수 있었습니다. 저는 DP로 풀었는데 @hi-rachel 님이 풀이한 방법으로도 풀이를 해봐야겠네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyer0705 저는 DP로도 풀어봐야겠네요. 감사합니다.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

디버깅용이라면 삭제하거나 로깅 수준으로 조절 필요할 것 같습니다

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