- 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