Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Leetcode Longest Palindromic Subsequence problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Longest Palindromic Subsequence problem solution Given a string s, find the longest palindromic subsequence’s length in s.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Leetcode Longest Palindromic Subsequence problem solution

Problem solution in Python.

class Solution:
    def longestPalindromeSubseq(self, s):

        if not s:
            return 0
        res = [[0 for i in range(len(s))] for i in range(len(s))]
        return self.helper(s, 0, len(s) - 1, res)

    def helper(self, inp, s, e, res):
        if s == e:
            return 1
        if s > e:
            return 0
        if res[s][e]:
            return res[s][e]
        if inp[s] == inp[e]:
            res[s][e] = 2 + self.helper(inp, s + 1, e - 1, res)
        else:
            res[s][e] = max(self.helper(inp, s + 1, e, res),
                         self.helper(inp, s, e - 1, res))
        return res[s][e]

Problem solution in Java.

class Solution {
    public int longestPalindromeSubseq(String s) {
       
        int n = s.length();
        StringBuilder b = new StringBuilder();         
        b.append(s);         
        b.reverse();
        return solve(s, b.toString(), n, n);
    
    }
    
  int solve(String str1, String str2, int n, int m){
        
        int[][] dp = new int[n + 1][m + 1];
        //initialize
        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < m + 1; j++){
                if(i == 0 || j == 0) dp[i][j] = 0;
            }
        }
        //main code
        for(int i = 1; i < n + 1; i++){
            for(int j = 1; j < m + 1; j++){
                if(str1.charAt(i - 1) == str2.charAt(j - 1)){
                    dp[i][j] = 1 + dp[i - 1][j - 1];
                }else{
                    dp[i][j] = Math.max(dp[i - 1][j],dp[i][j - 1]);
                }
            }
        }
        return dp[n][m];
    }
}

Problem solution in C++.

class Solution {
public:
    int longestPalindromeSubseq(string s) {
        int n = s.size();
        vector<vector<int>> dp(n+1,vector<int>(n+1,0));
        for(int i=1;i<dp.size();i++)
        {
            for(int j=1;j<dp.size();j++)
            {
                if(s[i-1] == s[n-j])
                {
                    dp[i][j] = 1 + dp[i-1][j-1];
                }else{
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[n][n];
    }
};

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes