Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def longestPalindrome(self, s: str) -> str:
- longestSubstring = ""
- substring = ""
- stringLength = len(s)
- if self.isPalindrome(s, stringLength):
- return s
- endIndex = stringLength + 1
- for i in reversed(range(endIndex)):
- for j in range(i):
- substringLength = i - j
- if len(longestSubstring) < substringLength:
- substring = s[j:i]
- if self.isPalindrome(substring, substringLength):
- longestSubstring = substring
- return longestSubstring
- def isPalindrome(self, s: str, stringLength: int) -> bool:
- """Takes a string and returns True if the string is a palindrome
- Arguments:
- s {str} -- The string to be tested
- stringLength {int} -- The length of the string
- Returns:
- bool -- True if it's a palindrome, False if it's not
- """
- if stringLength < 2:
- return True
- midpoint = stringLength // 2
- if stringLength % 2 != 0:
- # It's odd, so the midpoint doesn't matter
- if s[:midpoint] == s[:midpoint:-1]:
- return True
- return False
- else:
- # It's even
- if s[:midpoint] == s[: midpoint - 1 : -1]:
- return True
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement