commit 37dcccf8eb7126af495e0e82593df5cae7333327 parent f8ff0d66dd47a6168b8a846f27be71da852b6733 Author: Andrew Laack <andrew@laack.co> Date: Thu, 20 Aug 2026 22:49:54 -0500 Python solution Diffstat:
| A | longest-palindromic-substring/longest-palindromic-substring.py | | | 35 | +++++++++++++++++++++++++++++++++++ |
1 file changed, 35 insertions(+), 0 deletions(-)
diff --git a/longest-palindromic-substring/longest-palindromic-substring.py b/longest-palindromic-substring/longest-palindromic-substring.py @@ -0,0 +1,35 @@ +def is_palindrome(candidate): + left = 0 + right = len(candidate) - 1 + while left < right: + if candidate[left] != candidate[right]: + return False + left += 1 + right -= 1 + return True + +class Solution: + def longestPalindrome(self, s: str) -> str: + + longest = "" + + for i in range(len(s)): + left = i + right = i + while right < len(s) and left >= 0 and is_palindrome(s[left:right + 1]): + if (right - left) + 1 > len(longest): + longest = s[left:right + 1] + left -= 1 + right += 1 + + + for i in range(len(s)): + left = i + right = i + 1 + while right < len(s) and left >= 0 and is_palindrome(s[left:right + 1]): + if (right - left) + 1 > len(longest): + longest = s[left:right + 1] + left -= 1 + right += 1 + + return longest