LEETCODE - Longest Palindromic Substring

Coaspe·2021년 5월 4일
0
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-1]:
                left -= 1
                right += 1
            return s[left + 1:right -1]
        # 예외처리
        if len(s) < 2 or s == s[::-1]:
            return s
        
        result = ''
        # 슬라이딩 윈도우 우측으로 이동
        for i in range(len(s) - 1):
            result = max(result,
                        expand(i, i+1),
                        expand(i, i+2),
                        key=len)
        return result

arr[a:b] 이면 인덱스로 a ~ b-1 까지 출력

profile
https://github.com/Coaspe

0개의 댓글