From 4ac18f5dd4e32305b7be1d566b18ad3f632c33b2 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 6 Jul 2026 05:29:52 -0600 Subject: [PATCH] adding updates --- .../12_longest_palindrome_substring.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/my_project/interviews/google_top_exercises/round_1/12_longest_palindrome_substring.py diff --git a/src/my_project/interviews/google_top_exercises/round_1/12_longest_palindrome_substring.py b/src/my_project/interviews/google_top_exercises/round_1/12_longest_palindrome_substring.py new file mode 100644 index 00000000..f4673522 --- /dev/null +++ b/src/my_project/interviews/google_top_exercises/round_1/12_longest_palindrome_substring.py @@ -0,0 +1,25 @@ +class Solution: + def longestPalindrome(self, s: str) -> str: + if len(s) < 2: + return s + + start = 0 + max_len = 1 + + def expand(left: int, right: int) -> None: + nonlocal start, max_len + # Expand outward while the substring stays a palindrome + while left >= 0 and right < len(s) and s[left] == s[right]: + left -= 1 + right += 1 + # Loop exits one step too far, so the palindrome is s[left+1 : right] + length = right - left - 1 + if length > max_len: + max_len = length + start = left + 1 + + for i in range(len(s)): + expand(i, i) # odd-length center + expand(i, i + 1) # even-length center + + return s[start:start + max_len] \ No newline at end of file