Longest Palindromic Substring
Question
Example
Solution1
class Solution:
# @param {string} s input string
# @return {string} the longest palindromic substring
def longestPalindrome(self, s):
if s == None or len(s) == 1:
return s
# Write your code here
result = ''
for i in range(len(s)):
left, right = i, i + 1
while left >= 0 and right < len(s) and s[left] == s[right]:
if right - left + 1 > len(result):
result = s[left: right + 1]
left -= 1
right += 1
left, right = i - 1, i + 1
while left >= 0 and right < len(s) and s[left] == s[right]:
if right - left + 1 > len(result):
result = s[left: right + 1]
left -= 1
right += 1
return resultSolution 2
Solution 3
Solution 4
Last updated