박상길, 『파이썬 알고리즘 인터뷰』를 정리한 내용입니다.
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:
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]
answer = ''
for i in range(len(s) - 1):
answer = max(answer, expand(i, i+1), expand(i, i + 2), key=len)
return answer
[LeetCode] 3Sum | 투포인터 | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (1) | 2023.06.09 |
---|---|
[프로그래머스] 161988. 연속 펄스 부분 수열의 합| DP | 파이썬, 소스코드, 정답 (1) | 2023.06.04 |
[파이썬 알고리즘 인터뷰] 가장 흔한 단어 | 정규표현식 re.sub(), Counter (0) | 2023.05.31 |
[프로그래머스] 150365. 미로 탈출 명령어| 그리디, BFS | 파이썬, 소스코드, 정답 (0) | 2023.05.30 |
[프로그래머스] 150367. 표현 가능한 이진트리| 시뮬레이션, 구현 | 파이썬, 소스코드, 정답 (1) | 2023.05.26 |