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(object):
def longestPalindrome(self, s):
res = ""
for i in range(len(s)):
res = max(self.helper(s,i,i), self.helper(s,i,i+1), res, key=len)
return res
def helper(self,s,l,r):
while 0<=l and r < len(s) and s[l]==s[r]:
l-=1; r+=1
return s[l+1:r]
'💻 LEETCODE' 카테고리의 다른 글
[LEETCODE] ChatGPT에게 물어봐서 받은 EASY 목록 (1) | 2023.01.16 |
---|---|
[LEETCODE] Diagonal Traverse (0) | 2022.11.30 |
[LEETCODE] 1991. Find the Middle Index in Array (0) | 2022.11.24 |
[LEETCODE] 20. Valid Parentheses (0) | 2022.11.23 |
[LEETCODE] 14. Longest Common Prefix (0) | 2022.11.22 |
댓글