์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿฅš/๋ฌธ์ œํ’€์ด (Python)

[LeetCode] 5. longest_palindromic_substring (Medium) 2023/4/28

๐Ÿช„ํ•˜๋ฃจ๐Ÿช„ 2023. 4. 28. 23:08
728x90
๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„
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]์„ ์‚ฌ์šฉํ–ˆ๋Š”์ง€  ๋ฟ์ธ๋ฐ ์‹œ๊ฐ„์ด ์—ฌ์„ฏ๋ฐฐ์ •๋„ ์ฐจ์ด๊ฐ€ ๋‚˜์„œ ๋†€๋ž๋‹ค.

728x90