#5
Longest Palindromic Substring
medium· 1-D DPruns: 0Given a string s, return the longest palindromic substring in s.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 558
class Solution:
def longestPalindrome(self, s: str) -> str:
start, length = 0, 0
def expand(left: int, right: int) -> None:
nonlocal start, length
while left >= 0 and right < len(s) and s[left] == s[right]:
if right - left + 1 > length:
start = left
length = right - left + 1
left -= 1
right += 1
for i in range(len(s)):
expand(i, i)
expand(i, i + 1)
return s[start : start + length]
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.