Guest User

Untitled

a guest
Oct 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. # Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
  2.  
  3. # Example 1:
  4.  
  5. # Input: "babad"
  6. # Output: "bab"
  7. # Note: "aba" is also a valid answer.
  8. # Example 2:
  9.  
  10. # Input: "cbbd"
  11. # Output: "bb"
  12.  
  13. class Solution:
  14. def longestPalindrome(self, s):
  15. """
  16. :type s: str
  17. :rtype: str
  18. """
  19. if len(s)==1:return s
  20. def get_len(s,i,j):
  21. while i>=0 and j<len(s) and s[i]==s[j]:
  22. i-=1
  23. j+=1
  24. return s[i+1:j]
  25. res = ''
  26. for i in range(len(s)-1):
  27. for j in range(i,i+2):
  28. a = get_len(s,i,j)
  29. if len(a)>len(res):
  30. res = a
  31. return res
Add Comment
Please, Sign In to add comment