LeetCode 730: Count Different Palindromic Subsequences Solution

Master LeetCode problem 730 (Count Different Palindromic Subsequences), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

730. Count Different Palindromic Subsequences

Problem Explanation

Explanation

To solve this problem, we can use dynamic programming with a 2D array to keep track of the number of palindromic subsequences that end at each character and have the same starting and ending characters. We can expand this to consider subsequences with different starting and ending characters by recursively counting the number of palindromic subsequences within the substring between the two characters.

  1. Initialize a 2D array dp of size n x n x 4, where n is the length of the input string s and 4 represents the characters 'a', 'b', 'c', and 'd'.
  2. Iterate through the string s and update the dp array to count the number of palindromic subsequences that end at each character.
  3. For each character in the string, consider all possible characters to form a palindromic subsequence.
  4. Use recursive calls to count the number of palindromic subsequences within the substring between the two characters.
  5. Update the dp array with the counts.
  6. Sum up all the counts in the dp array to get the total number of different non-empty palindromic subsequences.

Solution Code

class Solution {
    public int countPalindromicSubsequences(String s) {
        int mod = 1000000007;
        int n = s.length();
        int[][][] dp = new int[n][n][4];

        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                for (int k = 0; k < 4; k++) {
                    char c = (char) ('a' + k);
                    if (j == i) {
                        if (s.charAt(i) == c) dp[i][j][k] = 1;
                        else dp[i][j][k] = 0;
                    } else {
                        if (s.charAt(i) != c) dp[i][j][k] = dp[i + 1][j][k];
                        else if (s.charAt(j) != c) dp[i][j][k] = dp[i][j - 1][k];
                        else {
                            dp[i][j][k] = 2;
                            if (j > i + 1) {
                                for (int m = 0; m < 4; m++) {
                                    dp[i][j][k] += dp[i + 1][j - 1][m];
                                    dp[i][j][k] %= mod;
                                }
                            }
                        }
                    }
                }
            }
        }

        int ans = 0;
        for (int k = 0; k < 4; k++) {
            ans += dp[0][n - 1][k];
            ans %= mod;
        }

        return ans;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 730 (Count Different Palindromic Subsequences)?

This page provides optimized solutions for LeetCode problem 730 (Count Different Palindromic Subsequences) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 730 (Count Different Palindromic Subsequences)?

The time complexity for LeetCode 730 (Count Different Palindromic Subsequences) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 730 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 730 in Java, C++, or Python.

Back to LeetCode Solutions