Leetcode[5] Longest Palindromic Substring
class Solution:
def helper(self,s,l,r):
while l>=0 and r<len(s) and s[l]==s[r]:
l=l-1
r=r+1
return s[l+1:r]
def longestPalindrome(self, s: str) -> str:
res=""
for i in range(len(s)):
tmp=self.helper(s,i,i)
if len(tmp)>len(res):
res=tmp
tmp=self.helper(s,i,i+1)
if len(tmp)>len(res):
res=tmp
return res
using dp:
class Solution:
def longestPalindrome(self, s: str) -> str:
if s=="":
return s
res=s[0]
dp=[[0 for i in range(len(s))] for i in range(len(s))]
for i in range(len(s)-1,-1,-1):
dp[i][i]=1
for j in range(i+1,len(s)):
if s[i]==s[j] and (dp[i+1][j-1] or j-i<2):
dp[i][j]=1
if dp[i][j] and len(res)<j-i+1:
res=s[i:j+1]
return res
Leetcode[647] Palindromic Substrings
128/130 passed, and time out of limit at last 2
class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
def helper(s,b,out,count):
for i in range(b,len(s)):
if b!=0 and i>b:
continue
out.append(s[i])
if out==out[::-1]:
self.count=self.count+1
helper(s,i+1,out,self.count)
out=[]
self.count=0
out=[]
helper(s,0,out,self.count)
return self.count
using iterate
class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
self.ret=0
def helper(l,h,s):
while l>=0 and h<len(s) and s[l]==s[h]:
self.ret=self.ret+1
l=l-1
h=h+1
for i in range(len(s)):
helper(i,i,s)
helper(i,i+1,s)
return self.ret
[9] Palindrome Number
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:
return False
if self.reverse(x)==x:
return True
return False
def reverse(self,x):
res=0
while x>0:
res=res*10+x%10
x=x/10
if res>sys.maxint:
return -1
return res
[125] Valid Palindrome
[131] Palindrome Partitioning
class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
ret=[]
tmp=[]
def helper(b,s):
if b>=len(s):
ret.append(tmp[:])
for i in range(b,len(s)):
cur=s[b:i+1]
if cur==cur[::-1]:
tmp.append(cur)
helper(i+1,s)
tmp.pop()
helper(0,s)
return ret
[680] Valid Palindrome II
my solution:
#
# @lc app=leetcode id=680 lang=python
#
# [680] Valid Palindrome II
#
class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
if not s:
return True
l=0
r=len(s)-1
flag=0
while l<=r:
if s[l]==s[r]:
l=l+1
r=r-1
elif flag==0 and s[l]!=s[r]:
if s[l+1]==s[r]:
l=l+1
elif s[l]==s[r-1]:
r=r-1
else:
return False
print l,r,s[l],s[r]
flag=1
elif flag==1 and s[l]!=s[r]:
print l,r,s[l],s[r]
return False
return True
⇒ 458/460 passed, failed on following case:
"aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga"
when
20 81 u u
21 80 p c
because when shift 1 from left and right are both fine for the case, so should traverse left and right both.
here is the correct solution:
class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
if not s:
return True
l=0
r=len(s)-1
while l<=r:
if s[l]==s[r]:
l=l+1
r=r-1
elif s[l]!=s[r]:
left=l
right=r-1
while left<=right: # del left
if s[left]!=s[right]:
break
left=left+1
right=right-1
if left>=right:
return True
#comes here,means del left don't walk through all
left=l+1
right=r
while left<=right: #del right
if s[left]!=s[right]:
return False
left=left+1
right=right-1
return True
return True
[516] Longest Palindromic Subsequence
using DFS, but TLE
class Solution(object):
def longestPalindromeSubseq(self, s):
"""
:type s: str
:rtype: int
"""
self.ret=0
def helper(s,out,b):
for i in range(b,len(s)):
out.append(s[i])
if out==out[::-1]:
if len(out)>self.ret:
self.ret=len(out)
helper(s,out,i+1)
out.pop()
out=[]
helper(s,out,0)
return self.ret
using DP:
class Solution(object):
def longestPalindromeSubseq(self, s):
"""
:type s: str
:rtype: int
"""
ret=0
dp=[[0 for i in range(len(s))] for i in range(len(s))]
for i in range(len(s)-1,-1,-1):
dp[i][i]=1
for j in range(i+1,len(s)):
if s[i]==s[j]:
dp[i][j]=dp[i+1][j-1]+2
else:
dp[i][j]=max(dp[i+1][j],dp[i][j-1])
return dp[0][len(s)-1]
if use 1 dimension dp array:
class Solution(object):
def longestPalindromeSubseq(self, s):
"""
:type s: str
:rtype: int
"""
ret=0
dp=[1 for i in range(len(s))]
for i in range(len(s)-1,-1,-1):
l=0
for j in range(i+1,len(s)):
t=dp[j]
if s[i]==s[j]:
dp[j]=l+2
l=max(l,t)
ret=max(dp)
return ret