Skip to main content
🌟 Loving the project? Support our open-source journey with a
Star on GitHub
!
Algo
Tutorials
Interview Engine
System Preparation
Verification Roadmap
Core Matrix Questions
Real-World Implementation
Contribution Tracker
Evaluation Pools
Code Challenges
Practice Arena
Concept Quizzes
Compiled Solutions
Community Hub
Telemetry & Systems
Contributors Wall
Global Leaderboard
Milestones & Badges
Infrastructure Patrons
Ecosystem Labs
Code Playground
Algorithm Visualizer
Backtracking & Grid Solver
Bitwise Operations
N-Queens Visualizer
Largest Rectangle (Histogram)
Maximal Rectangle (Matrix)
Max Building Height
Success Stories
Public Discussions
Extended Assets
FAQ
Blogs
English
English
हिन्दी
Search
Sign Up
Back to Challenges
Longest Palindromic Subsequence
Mark as Solved ✅
Hard
30 min
Problem
Solution
Pseudocode
🌐 Real-World
Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s.
Examples
Input:
s = 'bbbab'
Output:
4
One possible longest palindromic subsequence is 'bbbb'.
Input:
s = 'cbbd'
Output:
2
LPS is 'bb', length 2.
Constraints
•
1 <= s.length <= 1000
•
s consists of lowercase English letters.
Complexity Analysis
Time
O(n^2)
interval DP matrix scan.
Space
O(n^2)
matrix storing results for indices [i...j].
Complexity Deep Dive
Time:
O(n^2)
Poor
·
Space:
O(n^2)
Expand
Show Hint
Test Cases
#1 LPS of 4
Input:
"bbbab"
Expected:
4
#2 LPS of 2
Input:
"cbbd"
Expected:
2
JavaScript
Python
C++
Run Code
/** * @param {string} s * @return {number} */ function longestPalindromeSubseq(s) { // Your code here } console.log(longestPalindromeSubseq("bbbab")); // Expected: 4
Output
Click "Run Code" to see output here...