Back to Practice Dashboard
Competitive ProgrammingEasy
Longest Palindromic Subsequence
Learn how to solve the 'Longest Palindromic Subsequence' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function lps(s) that finds the length of the longest palindromic subsequence in a string 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.
Constraints
- •1 <= len(s) <= 1000
- •s consists of uppercase or lowercase English letters.
Examples
Example 1
Input
lps('BBABCBCAB')Output
7
Explanation
The longest palindromic subsequence has length 7, e.g., 'BABCBAB'.
Example 2
Input
lps('GEEKSFORGEEKS')Output
5
Explanation
The longest palindromic subsequence has length 5, e.g., 'EEKEE'.
Need a Hint?
Define subproblem states, establish the recurrence relation, and use memoization (top-down) or tabulation (bottom-up).
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.