Published:
Updated:

  • Reference
  • νˆ¬ν¬μΈν„°μΈ 것은 μ§κ°ν–ˆμ§€λ§Œ μ΄λ ‡κ²Œ μ–΄λ €μšΈ 쀄은 λͺ°λžλ‹€.


SolutionPermalink

class Solution:
    def longestPalindrome(self, s: str) -> str:
        if len(s) < 2 or s == s[::-1]:
            return s

        # νŒ°λ¦°λ“œλ‘¬ νŒλ³„ 및 νˆ¬ν¬μΈν„° ν™•μž₯
        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]

        # μŠ¬λΌμ΄μ‹± 우츑으둜 이동
        result = ''
        for i in range(len(s) - 1):
            # expand 각각 ν™€μˆ˜, 짝수
            result = max(result, expand(i, i + 1), expand(i, i + 2), key=len)

        return result

Leave a comment