Count Different Palindromic Subsequences - Problem

Given a string s, you need to find the number of different non-empty palindromic subsequences that can be formed from it. This is a challenging problem that combines string manipulation with dynamic programming!

๐ŸŽฏ What you need to do:

  • Count all possible palindromic subsequences (not substrings!)
  • A subsequence is formed by deleting zero or more characters while maintaining order
  • A sequence is palindromic if it reads the same forwards and backwards
  • Two subsequences are different if they differ at any position

โš ๏ธ Important: Since the answer can be very large, return it modulo 10^9 + 7.

Example: For string "bccb", the palindromic subsequences are: "b", "c", "cc", "bcb", "bccb" โ†’ Answer: 6

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "bccb"
โ€บ Output: 6
๐Ÿ’ก Note: The palindromic subsequences are: "b" (appears twice but we count unique strings), "c" (appears twice but counts as one), "cc", "bcb", "bccb". Total unique palindromic subsequences: 6
example_2.py โ€” All Same Character
$ Input: s = "abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba"
โ€บ Output: 104860361
๐Ÿ’ก Note: With repeating patterns, we get many different palindromic subsequences. The DP approach efficiently handles the exponential growth.
example_3.py โ€” Single Character
$ Input: s = "a"
โ€บ Output: 1
๐Ÿ’ก Note: Only one palindromic subsequence possible: "a"

Visualization

Tap to expand
Palindromic Subsequences: Building from Small to LargeString: "bccb"bccbStep 1: Single Characters (Length 1)1"b"1"c"1"c"1"b"Step 2: Two Characters (Length 2)"bc"2"cc"2"cb"2Final: Full String "bccb"Palindromes found:"b", "c", "cc", "bcb""bccb" โ†’ Total: 6Key DP Relations:When s[i] == s[j]:dp[i][j] = 2 ร— dp[i+1][j-1] + 2(with duplicate handling)When s[i] โ‰  s[j]:dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]๐Ÿ’ก Key InsightPalindromes have symmetric structure!We can count them by consideringendpoints and solving recursively
Understanding the Visualization
1
Initialize Base Cases
Every single character is a palindrome by itself
2
Build Up Ranges
For each substring, decide based on whether endpoints match
3
Handle Matches
When endpoints match, we can form new palindromes by wrapping them around inner palindromes
4
Handle Non-matches
When endpoints don't match, combine counts from two overlapping subproblems
5
Apply Modular Arithmetic
Keep results within bounds using modulo 10^9 + 7
Key Takeaway
๐ŸŽฏ Key Insight: By recognizing that palindromes have symmetric structure, we can use dynamic programming to count them efficiently in O(nยฒ) time instead of the brute force O(2^n) approach.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

We solve O(nยฒ) subproblems, each taking O(n) time in worst case

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

2D DP table to store results for all substrings

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 1000
  • s consists only of lowercase English letters
  • Return the answer modulo 109 + 7
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 25
48.3K Views
Medium-High Frequency
~25 min Avg. Time
1.9K 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