Longest Palindromic Subsequence - Problem

Given a string s, find the longest palindromic subsequence within it and return its length.

A subsequence is derived from a string by deleting some or no characters without changing the order of the remaining characters. Unlike substrings, subsequences don't need to be contiguous.

What makes this challenging? A palindrome reads the same forwards and backwards (like "racecar"). You need to find the longest possible palindrome you can form by picking characters from the original string while maintaining their relative order.

Example: In the string "bbbab", you can pick characters at positions 0,1,2 and 4 to form "bbbb" (length 4), which is the longest palindromic subsequence.

Input & Output

example_1.py — Basic Case
$ Input: s = "bbbab"
› Output: 4
šŸ’” Note: The longest palindromic subsequence is "bbbb" (taking characters at positions 0, 1, 2, and 4), which has length 4.
example_2.py — Mixed Characters
$ Input: s = "cbbd"
› Output: 2
šŸ’” Note: The longest palindromic subsequence is "bb" (taking characters at positions 1 and 2), which has length 2.
example_3.py — Single Character
$ Input: s = "a"
› Output: 1
šŸ’” Note: The string itself is a palindrome, so the longest palindromic subsequence is "a" with length 1.

Visualization

Tap to expand
Longest Palindromic Subsequence VisualizationString: "bbbab"Step 1: Initialize single charactersb1b1b1a1b1Step 2: Build length-2 substringsbb (2)bb (2)ab (1)Step 3: Optimal solution "bbbb"Longest: "bbbb"Length: 4DP Algorithm Steps1. Base Casedp[i][i] = 1 for all i(single chars are palindromes)2. Characters MatchIf s[i] == s[j]:dp[i][j] = dp[i+1][j-1] + 23. Characters Don't Matchdp[i][j] = max(dp[i+1][j], dp[i][j-1])4. Final AnswerReturn dp[0][n-1](entire string)Time: O(n²) | Space: O(n²) | Optimal dynamic programming solution
Understanding the Visualization
1
Single Characters
Every single character is a palindrome of length 1 - this forms our base case
2
Character Matching
When characters at both ends match, we can include both and add 2 to the inner result
3
No Match
When characters don't match, we try excluding either the first or last character and take the better result
4
Build Solution
Work from smaller substrings to larger ones until we solve for the entire string
Key Takeaway
šŸŽÆ Key Insight: Use dynamic programming to break the problem into overlapping subproblems. When characters at the ends match, include both and solve the inner problem. When they don't match, try both possibilities and take the better result.

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

We fill an nƗn DP table, each cell taking O(1) time to compute

n
2n
⚠ Quadratic Growth
Space Complexity
O(n²)

DP table of size nƗn to store intermediate results

n
2n
⚠ Quadratic Space

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists only of lowercase English letters
  • Note: Subsequences don't need to be contiguous
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
52.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen