diff --git a/longest-repeating-character-replacement/hi-rachel.py b/longest-repeating-character-replacement/hi-rachel.py index 6f0b220ae..aeeb690ba 100644 --- a/longest-repeating-character-replacement/hi-rachel.py +++ b/longest-repeating-character-replacement/hi-rachel.py @@ -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 + +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 diff --git a/palindromic-substrings/hi-rachel.py b/palindromic-substrings/hi-rachel.py index b397fa3b2..0fd0ebec2 100644 --- a/palindromic-substrings/hi-rachel.py +++ b/palindromic-substrings/hi-rachel.py @@ -55,3 +55,37 @@ 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) +SC: O(1) +""" + +class Solution: + def countSubstrings(self, s: str) -> int: + count = 0 + for center in range(len(s)): + 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