2484. Count Palindromic Subsequences LeetCode Solution

In this guide, you will get 2484. Count Palindromic Subsequences LeetCode Solution with the best time and space complexity. The solution to Count Palindromic Subsequences problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Palindromic Subsequences solution in C++
  4. Count Palindromic Subsequences solution in Java
  5. Count Palindromic Subsequences solution in Python
  6. Additional Resources
2484. Count Palindromic Subsequences LeetCode Solution image

Problem Statement of Count Palindromic Subsequences

Given a string of digits s, return the number of palindromic subsequences of s having length 5. Since the answer may be very large, return it modulo 109 + 7.
Note:

A string is palindromic if it reads the same forward and backward.
A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

Example 1:

Input: s = “103301”
Output: 2
Explanation:
There are 6 possible subsequences of length 5: “10330”,”10331″,”10301″,”10301″,”13301″,”03301″.
Two of them (both equal to “10301”) are palindromic.

Example 2:

Input: s = “0000000”
Output: 21
Explanation: All 21 subsequences are “00000”, which is palindromic.

Example 3:

Input: s = “9999900000”
Output: 2
Explanation: The only two palindromic subsequences are “99999” and “00000”.

Constraints:

1 <= s.length <= 104
s consists of digits.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2484. Count Palindromic Subsequences LeetCode Solution in C++

class Solution {
 public:
  int countPalindromes(string s) {
    constexpr int kMod = 1'000'000'007;
    constexpr int kPatternSize = 5;
    long ans = 0;

    for (char a = '0'; a <= '9'; ++a)
      for (char b = '0'; b <= '9'; ++b) {
        const vector<char> pattern{a, b, '.', b, a};
        // dp[i] := the number of subsequences of pattern[i..n) in s, where
        // pattern[2] can be any character
        vector<long> dp(kPatternSize + 1);
        dp.back() = 1;
        for (const char c : s)
          for (int i = 0; i < kPatternSize; ++i)
            if (pattern[i] == '.' || pattern[i] == c)
              dp[i] += dp[i + 1];
        ans += dp[0];
        ans %= kMod;
      }

    return ans;
  }
};
/* code provided by PROGIEZ */

2484. Count Palindromic Subsequences LeetCode Solution in Java

class Solution {
  public int countPalindromes(String s) {
    final int kMod = 1_000_000_007;
    final int kPatternSize = 5;
    long ans = 0;

    for (char a = '0'; a <= '9'; ++a)
      for (char b = '0'; b <= '9'; ++b) {
        char[] pattern = {a, b, '.', b, a};
        // dp[i] := the number of subsequences of pattern[i..n) in s, where
        // pattern[2] can be any character
        long[] dp = new long[kPatternSize + 1];
        dp[kPatternSize] = 1;
        for (final char c : s.toCharArray())
          for (int i = 0; i < kPatternSize; ++i)
            if (pattern[i] == '.' || pattern[i] == c)
              dp[i] += dp[i + 1];
        ans += dp[0];
        ans %= kMod;
      }

    return (int) ans;
  }
}
// code provided by PROGIEZ

2484. Count Palindromic Subsequences LeetCode Solution in Python

class Solution:
  def countPalindromes(self, s: str) -> int:
    kMod = 1_000_000_007
    ans = 0

    for a in range(10):
      for b in range(10):
        pattern = f'{a}{b}.{b}{a}'
        # dp[i] := the number of subsequences of pattern[i..n) in s, where
        # pattern[2] can be any character
        dp = [0] * 5 + [1]
        for c in s:
          for i, p in enumerate(pattern):
            if p == '.' or p == c:
              dp[i] += dp[i + 1]
        ans += dp[0]
        ans %= kMod

    return ans
# code by PROGIEZ

Additional Resources

See also  2952. Minimum Number of Coins to be Added LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.