We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
if len(s)==1:
return 2
if k==0:
return 26*(len(s)+1)
def lcs(x,y,s1,s2):
dp = [[-1] * (y + 1) for _ in range(x + 1)]
for i in range(x+1):
for j in range(y+1):
if i==0 or j==0:
dp[i][j] = 0
elif (s1[i-1]==s2[j-1]):
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i][j-1],dp[i-1][j] )
return dp[x][y]
n = len(s)
lps = lcs(n,n,s,s[::-1])
alpha = {}
for i in s:
alpha[i] =1
count = 0
for i in alpha:
for j in range(n):
new_str = s[:j] + i + s[j:]
new_len = len(new_str)
if lcs(n+1,n+1,new_str,new_str[::-1]) >= lps+k:
count+=1
return count`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Longest Palindromic Subsequence
You are viewing a single comment's thread. Return to all comments →
two test cases pass can anyone fix this
`def longestPalindromicSubsequence(s, k):