
									 Problem
								
								
									 Solution
								
								
									 Submissions
								
								
							Different Palindromic Subsequences
								Certification: Advanced Level
								Accuracy: 100%
								Submissions: 1
								Points: 15
							
							Write a Java program to count the number of different palindromic subsequences in a given string. 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. A palindrome is a string that reads the same backward as forward. The answer should be computed modulo 10^9 + 7 because the result could be very large.
Note: The empty string is not considered a palindromic subsequence.
Example 1
- Input: s = "bccb"
 - Output: 6
 - Explanation: The different palindromic subsequences are: "b", "c", "bb", "cc", "bcb", "bccb". "b" appears twice in the string, but is counted only once. "c" appears twice in the string, but is counted only once.
 
Example 2
- Input: s = "abcdabcba"
 - Output: 15
 - Explanation: The different palindromic subsequences are: "a", "b", "c", "d", "aa", "bb", "cc", "aba", "aca", "ada", "aba", "aca", "bcb", "abcba", "abcdabcba". "a", "b", and "c" each appear multiple times but are counted only once.
 
Constraints
- The input string length will not exceed 1000
 - The input string consists of lowercase English letters only
 - Time Complexity: O(n^2), where n is the length of the string
 - Space Complexity: O(n^2)
 - The result should be computed modulo 10^9 + 7
 
Editorial
									
												
My Submissions
										All Solutions
									| Lang | Status | Date | Code | 
|---|---|---|---|
| You do not have any submissions for this problem. | |||
| User | Lang | Status | Date | Code | 
|---|---|---|---|---|
| No submissions found. | ||||
Solution Hints
- Use dynamic programming to solve this problem
 - Define dp[i][j] as the number of different palindromic subsequences in the substring s[i...j]
 - Base case: dp[i][i] = 1 for each character
 - If s[i] == s[j], the number of palindromic subsequences will depend on whether s[i] appears within the substring s[i+1...j-1]
 - If s[i] != s[j], we need to consider palindromic subsequences in s[i...j-1] and s[i+1...j], avoiding double counting
 - Consider edge cases carefully when characters appear multiple times
 - Remember to take modulo 10^9 + 7 for each calculation