Tutorialspoint
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
ArraysDynamic Programming HCL TechnologiesAdobe
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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

Steps to solve by this approach:

 Step 1: Create a 2D DP array where dp[i][j] represents the number of different palindromic subsequences in the substring s[i...j].

 Step 2: Initialize the base case: dp[i][i] = 1 for each character (each character is a palindrome).
 Step 3: Fill the DP table by considering substrings of increasing length.
 Step 4: For each substring s[i...j], if s[i] != s[j], calculate dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] to avoid double counting.
 Step 5: If s[i] == s[j], the result depends on whether s[i] appears within the substring s[i+1...j-1].
 Step 6: Check three cases: no occurrence, one occurrence, or multiple occurrences of s[i] in the middle, and calculate accordingly.
 Step 7: Take modulo 10^9 + 7 at each step to prevent integer overflow.

Submitted Code :