Problem Description
Given a string s, return the number of different non-empty palindromic subsequences in s. Since the answer may be very large, return it modulo 10^9 + 7. A subsequence of a string is obtained by deleting zero or more characters from the string. A sequence is palindromic if it is equal to the sequence reversed. Two sequences a1, a2, ... and b1, b2, ... are different if there is some i for which ai != bi.
Key Insights
- A palindromic subsequence reads the same forwards and backwards.
- Dynamic programming can be used to efficiently count unique palindromic subsequences.
- Identifying and handling overlapping subsequences is crucial for the correct count.
- The result must be computed modulo 10^9 + 7 to avoid overflow.
Space and Time Complexity
Time Complexity: O(n^2)
Space Complexity: O(n^2)
Solution
To solve this problem, we can use a dynamic programming approach. We will maintain a 2D array dp
where dp[i][j]
represents the count of different palindromic subsequences in the substring s[i...j]
. The algorithm can be broken down into the following steps:
- Initialization: For every single character in the string, initialize
dp[i][i]
to 1 since each character is a palindromic subsequence by itself. - Filling the DP Table:
- For each length of the substring from 2 to n, iterate through all possible starting indices.
- If the characters at the two ends of the substring are the same, we can form new palindromic subsequences by considering the subsequences formed by the inner substring plus the new palindromic sequences formed by these two characters.
- If the characters are different, we will count the unique subsequences from both sides, subtracting the overlapping subsequences to avoid double counting.
- Result: The value at
dp[0][n-1]
will give the count of different palindromic subsequences for the entire string.