Skip to main content
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
Visualizers
Algorithm Visualizer
Big-O Runtime Benchmark Playground
Time Complexity Visualizer
Backtracking & Grid Solver
Bitwise Operations
N-Queens Visualizer
Largest Rectangle
Maximal Rectangle
Max Building Height
Community & Ecosystem
Telemetry & Systems
Contributors Wall
Global Leaderboard
Milestones & Badges
Become a Sponsor
Ecosystem Labs
Code Playground
Success Stories
Public Discussions
Extended Assets
Favorites
FAQ
Blog
Algorithm Digest
English
English
हिन्दी
Search
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...
?