Given a string s, return the longest palindromic substring 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:
def expand(left,right):
while left >= 0 and right < len(s) and s[left] == s[right]:
left-=1
right+=1
print(left,right, s[left+1:right])
return s[left+1:right]
longest = ""
for i in range(len(s)):
odd = expand(i,i)
if len(odd) > len(longest):
longest = odd
even = expand(i,i+1)
if len(even) > len(longest):
longest = even
return longest
'LEETCODE' 카테고리의 다른 글
#11. Container With Most Water [Two Ptr] (0) | 2023.10.12 |
---|---|
#7. Reverse Integer (0) | 2023.10.10 |
# 17. Letter Combinations of a Phone Number [BACKTRACK, DFS] (0) | 2023.10.08 |
#19. Remove Nth Node From End of List [Sliding window] (0) | 2023.10.07 |
#3. Longest Substring Without Repeating Characters [MEDIUM] (0) | 2023.10.07 |