Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
- # Example 1:
- # Input: "babad"
- # Output: "bab"
- # Note: "aba" is also a valid answer.
- # Example 2:
- # Input: "cbbd"
- # Output: "bb"
- class Solution:
- def longestPalindrome(self, s):
- """
- :type s: str
- :rtype: str
- """
- if len(s)==1:return s
- def get_len(s,i,j):
- while i>=0 and j<len(s) and s[i]==s[j]:
- i-=1
- j+=1
- return s[i+1:j]
- res = ''
- for i in range(len(s)-1):
- for j in range(i,i+2):
- a = get_len(s,i,j)
- if len(a)>len(res):
- res = a
- return res
Add Comment
Please, Sign In to add comment