Sort by

recency

|

9 Discussions

|

  • + 0 comments

    Here is my solution in java, C, C++, Csharp HackerRank Longest Palindromic Subsequence

  • + 0 comments

    two test cases pass can anyone fix this

    `def longestPalindromicSubsequence(s, k):

    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`
    
  • + 0 comments

    here is Longest Palindromic Subsequence problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-longest-palindromic-subsequence-problem-solution.html

  • + 0 comments

    Best solution for C/C++ Java and Python. Check it now ~!

  • + 0 comments

    How can be decreased the time complexity?