- Reference
- ν¬ν¬μΈν°μΈ κ²μ μ§κ°νμ§λ§ μ΄λ κ² μ΄λ €μΈ μ€μ λͺ°λλ€.
Solution
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