Last active
July 28, 2021 07:53
-
-
Save cjnghn/56344b9c3454ea544d31a90fa6b22fb1 to your computer and use it in GitHub Desktop.
5. Longest Palindromic Substring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def longestPalindrome(self, s: str) -> str: | |
def extend(l, r): # 구현 연습 필요!~ | |
while l >= 0 and r < n and s[l] == s[r]: | |
l -= 1; r += 1 | |
return s[l + 1:r] | |
res, n = "", len(s) | |
for i in range(n): | |
# 슬라이딩 윈도우를 떠올리다가 두 케이스로 나눔. | |
# 홀수, "aba" | |
tmp = extend(i, i) | |
if len(tmp) > len(res): | |
res = tmp | |
# 짝수, "abba" | |
tmp = extend(i, i + 1) | |
if len(tmp) > len(res): | |
res = tmp | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment