String
5. Longest Palindromic Substring
1 min readApr 11, 2020
this is WRONG, we can NOT use binary search here.
The following code can pass half of the test case and it is not correct as the number of length of
for example, we have the number of palindromic as 1 and 3 and we don’t have 2.
“aba”
class Solution:
def longestPalindrome(self, s: str) -> str:
if not s:return ""
def good(m):
for i in range(len(s)):
if i+m<=len(s) and s[i:i+m][::-1] == s[i:i+m]:return True
return False
def binary():
l, r = 1, len(s)
while l<r:
m = (l+r+1)//2
if good(m):
l = m
else:
r = m-1
return l
m = binary()
for i in range(len(s)):
if s[i:i+m][::-1] == s[i:i+m]:return s[i:i+m]