|
| 1 | +from unittest import TestCase, main |
| 2 | + |
| 3 | + |
| 4 | +class Solution: |
| 5 | + def longestPalindrome(self, s: str) -> str: |
| 6 | + return self.solve_manacher_algorithm(s) |
| 7 | + |
| 8 | + """ |
| 9 | + Runtime: 47 ms (Beats 96.97%) |
| 10 | + Time Complexity: O(n ^ 3) |
| 11 | + - s의 길이를 n이라 하면, s의 길이 - 1 만큼 조회하는데 O(n - 1) |
| 12 | + - 각 문자마다 sliding_window를 2회 호출하는데, 각 호출마다 최대 s의 길이만큼 반복하므로, * 2 * O(n), upper bound |
| 13 | + - 반복 후 s를 slicing하는데 최대 * O(n), upper bound |
| 14 | + > O(n - 1) * (2 * O(n)) * O(n) ~= O(n ^ 3) |
| 15 | +
|
| 16 | + Memory: 16.54 MB (Beats 88.85%) |
| 17 | + Space Complexity: O(n) |
| 18 | + - sliding_window의 결과로 생성되는 문자열의 최대 길이는 n이고, 조회마다 2회 생성되므로 2 * O(n), upper bound |
| 19 | + > 2 * O(n) ~= O(n) |
| 20 | + """ |
| 21 | + def solve_sliding_window(self, s: str) -> str: |
| 22 | + |
| 23 | + def sliding_window(left: int, right: int) -> str: |
| 24 | + while 0 <= left and right < len(s) and s[left] == s[right - 1]: |
| 25 | + left -= 1 |
| 26 | + right += 1 |
| 27 | + |
| 28 | + return s[left + 1: right - 1] |
| 29 | + |
| 30 | + if len(s) < 2 or s == s[::-1]: |
| 31 | + return s |
| 32 | + |
| 33 | + result = '' |
| 34 | + for i in range(len(s) - 1): |
| 35 | + result = max(result, sliding_window(i, i + 1), sliding_window(i, i + 2), key=len) |
| 36 | + |
| 37 | + return result |
| 38 | + |
| 39 | + """ |
| 40 | + Runtime: 36 ms (Beats 98.09%) |
| 41 | + Time Complexity: O(n ^ 2) |
| 42 | + - s의 길이를 n이라 하면, s의 길이 - 1 만큼 조회하는데 O(n - 1) |
| 43 | + - 각 문자마다 two_pointer 2회 호출하는데, 각 호출마다 최대 s의 길이만큼 반복하므로, * 2 * O(n), upper bound |
| 44 | + > O(n - 1) * (2 * O(n)) ~= O(n ^ 2) |
| 45 | +
|
| 46 | + Memory: 16.85 MB (Beats 24.42%) |
| 47 | + Space Complexity: O(1) |
| 48 | + > 모든 변수는 result를 제외하고 인덱스를 위한 정수 변수만 사용하므로 O(1) |
| 49 | + """ |
| 50 | + def solve_two_pointer(self, s: str) -> str: |
| 51 | + |
| 52 | + if len(s) < 2 or s == s[::-1]: |
| 53 | + return s |
| 54 | + |
| 55 | + def two_pointer(left: int, right: int) -> (int, int): |
| 56 | + while left >= 0 and right < len(s) and s[left] == s[right]: |
| 57 | + left -= 1 |
| 58 | + right += 1 |
| 59 | + |
| 60 | + return left + 1, right - 1 |
| 61 | + |
| 62 | + start, end = 0, 0 |
| 63 | + for i in range(len(s) - 1): |
| 64 | + first_left, first_right = two_pointer(i, i) |
| 65 | + second_left, second_right = two_pointer(i, i + 1) |
| 66 | + |
| 67 | + if first_right - first_left > end - start: |
| 68 | + start, end = first_left, first_right |
| 69 | + if second_right - second_left > end - start: |
| 70 | + start, end = second_left, second_right |
| 71 | + |
| 72 | + return s[start: end + 1] |
| 73 | + |
| 74 | + """ |
| 75 | + Time Complexity: O(n) |
| 76 | + Space Complexity: O(n) |
| 77 | + """ |
| 78 | + def solve_manacher_algorithm(self, s: str) -> str: |
| 79 | + SEPARATOR = '@' |
| 80 | + # Step 1: Transform the string |
| 81 | + t = SEPARATOR + SEPARATOR.join(s) + SEPARATOR |
| 82 | + n = len(t) |
| 83 | + p = [0] * n |
| 84 | + center = right = 0 # Center and right boundary |
| 85 | + max_length = 0 |
| 86 | + max_center = 0 |
| 87 | + |
| 88 | + # Step 2: Calculate palindrome radius for each character |
| 89 | + for c in range(n): |
| 90 | + # Use previously calculated information (symmetry) |
| 91 | + if c < right: |
| 92 | + p[c] = min(p[2 * center - c], right - c) |
| 93 | + |
| 94 | + # Try to expand around i |
| 95 | + while (0 <= c - p[c] - 1 and c + p[c] + 1 < n) and (t[c - p[c] - 1] == t[c + p[c] + 1]): |
| 96 | + p[c] += 1 |
| 97 | + |
| 98 | + # Update center and right boundary if expanded beyond current right |
| 99 | + if c + p[c] > right: |
| 100 | + center = c |
| 101 | + right = c + p[c] |
| 102 | + |
| 103 | + # Update max palindrome length and center |
| 104 | + if p[c] > max_length: |
| 105 | + max_length = p[c] |
| 106 | + max_center = c |
| 107 | + |
| 108 | + # Step 3: Extract the original string's palindrome |
| 109 | + start = (max_center - max_length) // 2 |
| 110 | + return s[start:start + max_length] |
| 111 | + |
| 112 | + |
| 113 | +class _LeetCodeTestCases(TestCase): |
| 114 | + def test_1(self): |
| 115 | + s = "babad" |
| 116 | + output = "bab" |
| 117 | + self.assertEqual(Solution().longestPalindrome(s), output) |
| 118 | + |
| 119 | + def test_2(self): |
| 120 | + s = "cbbd" |
| 121 | + output = "bb" |
| 122 | + self.assertEqual(Solution().longestPalindrome(s), output) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + main() |
0 commit comments