07 Jan 5. Longest Palindromic Substring
Given a string s
, return the longest palindromicsubstring in s
.
Example 1:
Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd" Output: "bb"
Constraints:
1 <= s.length <= 1000
s
consist of only digits and English letters.
class Solution:
def longestPalindrome(self, s: str) -> str:
n = len(s)
def getLen(left, right):
while left >=0 and right < n and s[left] == s[right]:
left -=1
right +=1
return right - left - 1
start = 0
length = 0
for i in range(n):
print("------------------------------")
print(i)
cur = max(getLen(i, i), getLen(i, i+1))
print(cur)
if cur <= length:
continue
length = cur
start = i - (cur-1) // 2
print("end------------------------------")
print(s[start:start+length])
return s[start:start+length]
No Comments