class Solution:
def longestPalindrome(self, s: str) -> str:
def expand(left: int, right: int) -> str:
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return s[left + 1:right]
if len(s) < 2 or s == s[::-1]:
return s
result = ""
for i in range(len(s)):
result = max(result,
expand(i, i + 1),
expand(i, i + 2),
key=lambda x: len(x))
return result
- 짝수문자, 홀수문자를 나눠서 고려한다
- 투포인터를 활용한다.
palindrome 문자는 짝수/홀수길이를 가질 수 있기 때문에
고려해야할 경우는 짝수
와 홀수
문자이다.
여기서 포인트는 짝수문자와 홀수문자를 동시에 고려해야한다.
투포인터를 통해 확인할 문자의 범위를 넓혀간다