मुख्य कंटेंट तक स्किप करें
🌟 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
Recursion Visualizer
Success Stories
Public Discussions
Extended Assets
FAQ
Blogs
हिन्दी
English
हिन्दी
खोज करें
Sign Up
Back to Challenges
Longest Palindromic Subsequence
Hard
30 min
problem
solution
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].
Show Hint
Test Cases
#1 LPS of 4
Input:
"bbbab"
Expected:
4
#2 LPS of 2
Input:
"cbbd"
Expected:
2
JavaScript
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...