๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
5. longest-palindromic-substring | 32.4% | Medium |
Longest Palindromic Substring - LeetCode
Can you solve this real interview question? Longest Palindromic Substring - Given a string s, return the longest palindromic substring in s. Example 1: Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2: Input: s = "cb
leetcode.com
๋ฌธ์ ์์ฝ
๊ฐ์ฅ ๊ธด ํฐ๋ฆฐ๋๋กฌ ๋ถ๋ถ ๋ฌธ์์ด์ ๊ตฌํ์ฌ๋ผ
์กฐ๊ฑด 1) ๋ฌธ์์ด์ด ์
๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค
์กฐ๊ฑด 2) ํฐ๋ฆฐ๋๋กฌ์ด๋ ์์์๋ถํฐ ์ฝ์ด์จ ๊ฒ๊ณผ ๋ค์์๋ถํฐ ์ฝ์ด์จ ๊ฒ์ด ๊ฐ์ ๋ฌธ์์ด์ ๋งํ๋ค
Step0. ๊ทน๋จ์ ์ธ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ
๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 0์ผ ๊ฒฝ์ฐ -> ํด๋น ๋ฌธ์์ด ๋ฐํ
๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 1์ผ ๊ฒฝ์ฐ -> ํด๋น ๋ฌธ์์ด ๋ฐํ
๋ฌธ์์ด ์์ฒด๊ฐ ํฐ๋ฆฐ๋๋กฌ ์ผ ๊ฒฝ์ฐ -> ํด๋น ๋ฌธ์์ด ๋ฐํ
Step1. ์ ์ผ ๊ธด ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ์ธ๋ ๋ฌธ์ ์ด๋ฏ๋ก ์ฃผ์ด์ง ๋ฌธ์์ด์ ๊ธธ์ด๋ถํฐ ์ฐจ๋ก๋๋ก ๊ณ์ฐ
๊ตฌํ (4406 ms)
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
#Step 0
if len(s)<2 or s==s[::-1]:
return s
#Step 1
longest_palindrome=""
nowlen=len(s)
while(nowlen>=2):
for left_index in range(0, len(s)-nowlen+1):
nowstr=s[left_index:left_index+nowlen]
if(nowstr==nowstr[::-1]):
return nowstr
nowlen-=1
return s[0]
์๋๊ฐ ๋๋ฆฐ ์ด์ : while๋ฌธ์ฅ ์์ for ๋ฌธ์ฅ ์์ if ๋ฌธ์ฅ์ด ์๊ธฐ ๋๋ฌธ.
์งํฅํ๋ ํ์ด - ํฌ ํฌ์ธํฐ
์ฐพ์ ํฐ๋ฆฐ๋๋กฌ์ ์ค์ฌ์ผ๋ก ์ ์์ผ๋ก ํ์ฅํ๋ ๋๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ด์ฉ
- ์ง์ ์นธ์ palindrome ํ๋จ
- ํ์ ์นธ์ palindrome ํ๋จ
Step1. ์ง์, ํ์ ํฐ๋ฆฐ๋๋กฌ์ ์ฐพ๋๋ค.
๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 0์ผ ๊ฒฝ์ฐ -> ํด๋น ๋ฌธ์์ด ๋ฐํ
๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 1์ผ ๊ฒฝ์ฐ -> ํด๋น ๋ฌธ์์ด ๋ฐํ
๋ฌธ์์ด ์์ฒด๊ฐ ํฐ๋ฆฐ๋๋กฌ ์ผ ๊ฒฝ์ฐ -> ํด๋น ๋ฌธ์์ด ๋ฐํ
Step2. ํด๋น ํฐ๋ฆฐ๋๋กฌ์ ๊ธฐ์ค์ผ๋ก ์์์ ํ์ฅํด ํฐ๋ฆฐ๋๋กฌ์ธ์ง ํ์ธ -> ์๋ก์ด ํจ์ ์ ์
iterable ๊ฐ์ฒด ์์์ key ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ์ผ ํฐ ๊ฐ์ ์ถ๋ ฅํ๋ ํจ์ | max(iterable, key=None) |
๊ตฌํ(621ms)
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
# step 2
def increase_index(left:int, right:int):
while(left>=0 and right<=len(s) and s[left]==s[right-1]):
left-=1
right+=1
return s[left+1: right-1]
# step 0
if len(s)<2 and s==s[::-1]:
return s
longest_palindrome=''
for left_index in range(0, len(s)-1):
# step 1 ๊ธธ์ด๊ฐ ํ์, ์ง์์ธ ํฌ์ธํฐ ๋๋ ์ ํ๋จ
longest_palindrome=max(longest_palindrome, increase_index(left_index, left_index+2),increase_index(left_index, left_index+3), key=len)
return longest_palindrome
์ฑ
์์์ ๊ตฌํ(118ms)
class Solution:
def longestPalindrome(self, s: str) -> str:
def increase_index(left:int, right:int)->str:
#step2
while left>=0 and right<len(s) and s[left]==s[right]:
left-=1
right+=1
return s[left+1:right]
longest_palindrome=''
#step 0
if len(s)<2 or s==s[::-1]:
return s
#step 1
for left_index in range(0, len(s)-1):
longest_palindrome=max(longest_palindrome, increase_index(left_index, left_index+1), increase_index(left_index, left_index+2), key=len)
return longest_palindrome
๋ ๋ฐฉ๋ฒ์ ์ฐจ์ด์ ์ while ์กฐ๊ฑด์ ์ s[right-1] s[right]์ผ๋ก ์ฐ์ฐ๊ณผ, s[left+1, right-1] s[left+1, right]์ ์ฌ์ฉํ๋์ง ๋ฟ์ธ๋ฐ ์๊ฐ์ด ์ฌ์ฏ๋ฐฐ์ ๋ ์ฐจ์ด๊ฐ ๋์ ๋๋๋ค.
'์๊ณ ๋ฆฌ์ฆ๐ฅ > ๋ฌธ์ ํ์ด (Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] 2. ์ฝ๋ฉํ ์คํธ์์ ์ฌ์ฉ๋๋ Python ๊ธฐ๋ณธ (0) | 2023.04.30 |
---|---|
[LeetCode] 1. two_sum (Easy) 2023/4/29 (0) | 2023.04.29 |
[LeetCode] 49. group-anagrams (Medium) 2023/4/28 (0) | 2023.04.28 |
[LeetCode] 937. reorder-data-in-log-files (Easy) 2023/4/27 (0) | 2023.04.27 |
[LeetCode] 344. reverse-string (Easy) 2023/4/27 (0) | 2023.04.27 |