[알고리즘] leetcode longest palindromic substring python

진실·2022년 11월 23일
0

알고리즘

목록 보기
17/22
post-custom-banner

https://leetcode.com/problems/longest-palindromic-substring

class Solution:
    def longestPalindrome(self, s: str) -> str:
        def findPalindrome(s: str, left, right) -> str:
            while left >= 0 and right < len(s) and s[left] == s[right]:
                left -= 1
                right += 1
            return s[left + 1 : right]

        maxLen = 0
        maxStr = ""
        for i in range(len(s)):
            oddPalindrome = findPalindrome(s, i, i)
            evenPalindrome = findPalindrome(s, i, i + 1)
            if maxLen < len(oddPalindrome):
                maxLen = len(oddPalindrome)
                maxStr = oddPalindrome

            if maxLen < len(evenPalindrome):
                maxLen = len(evenPalindrome)
                maxStr = evenPalindrome

        return maxStr
profile
반갑습니다.
post-custom-banner

0개의 댓글