LC005 - Longest Palindromic Substring
Problem
Example
Solution
Naive
def longestPalindrome(self, s: str) -> str:
for length in range(len(s), 0, -1):
for start in range(len(s) - length + 1):
if self.check(s, start, start + length):
return s[start : start + length]
return ""
def check (self, s, i, j):
lbound = i
rbound = j - 1
while lbound < rbound:
if s[lbound] != s[rbound]:
return False
lbound += 1
rbound -= 1
return TrueImproved
Optimal
Last updated