파비의 매일매일 공부기록

2023.10.27 Today's Challenge 본문

Problem Solving/LeetCode

2023.10.27 Today's Challenge

fabichoi 2023. 10. 27. 23:45

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

 

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

브루트-포스로 푸는 문제

class Solution:
    def longestPalindrome(self, s: str) -> str:
        def check(i, j):
            l, r = i, j - 1

            while l < r:
                if s[l] != s[r]:
                    return False
                l += 1
                r -= 1
            
            return True
        
        for length in range(len(s), 0, -1):
            for start in range(len(s) - length + 1):
                if check(start, start + length):
                    return s[start:start + length]
        
        return ""
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.10.29 Today's Challenge  (0) 2023.10.29
2023.10.28 Today's Challenge  (0) 2023.10.28
2023.10.26 Today's Challenge  (0) 2023.10.26
2023.10.25 Today's Challenge  (0) 2023.10.25
2023.10.24 Today's Challenge  (0) 2023.10.24
Comments