Explorer
Data Structures & Algorithms

Count total palindromic subsequences using dynamic programming.

Problem Statement

Given a string `s` of lowercase characters, find the number of palindromic subsequences (need not be distinct). Return the answer modulo `10^9 + 7`.

Examples

Input: s = "abcd"

Output: 4

Explanation: Combining the input according to Count Palindromic Subsequences logic yields 4.

Input: s = "aab"

Output: 4

Explanation: Combining the input according to Count Palindromic Subsequences logic yields 4.

Input: s = "aaaa"

Output: 15

Explanation: Combining the input according to Count Palindromic Subsequences logic yields 15.

Complexity

Time Complexity: O(N²)

Space Complexity: O(N²)

Hints

💡 Hint 1: Use 2D dynamic programming: dp[i][j] is the count of palindromic subsequences in s[i...j]. 💡 Hint 2: Base cases: dp[i][i] = 1 for all single characters. 💡 Hint 3: Transition: If s[i] === s[j], dp[i][j] = dp[i+1][j] + dp[i][j-1] + 1. Otherwise dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]. Modulo 10^9 + 7.

Editorial & Approach

Problem Overview & Intuition

Let dp[i][j] be the number of palindromic subsequences in s[i...j]. By the inclusion-exclusion principle: when s[i] !== s[j], count is dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]. When s[i] === s[j], any palindrome in s[i+1...j-1] can be wrapped by s[i] and s[j], plus the pair {s[i], s[j]} itself.

Step-by-Step Approach

  1. Initialize dp[n][n] table with BigInt values modulo 10^9 + 7.
  2. Base cases: dp[i][i] = 1.
  3. Iterate interval length len from 2 to n.
  4. If s[i] === s[j]: dp[i][j] = (dp[i + 1][j] + dp[i][j - 1] + 1) % MOD.
  5. Else: dp[i][j] = (dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + MOD) % MOD.
  6. Return Number(dp[0][n - 1]).

Optimal Implementation (JavaScript)

function countPalindromicSubsequences(s) {
  const n = s.length;
  const MOD = 1000000007n;
  const dp = Array.from({ length: n }, () => new Array(n).fill(0n));
  for (let i = 0; i < n; i++) dp[i][i] = 1n;
  for (let len = 2; len <= n; len++) {
    for (let i = 0; i <= n - len; i++) {
      const j = i + len - 1;
      if (s[i] === s[j]) {
        dp[i][j] = (dp[i + 1][j] + dp[i][j - 1] + 1n) % MOD;
      } else {
        dp[i][j] = (dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + MOD) % MOD;
      }
    }
  }
  return Number(dp[0][n - 1]);
}

Complexity Analysis

Time Complexity O(N²) — filling N x N dynamic programming table.
Space Complexity O(N²) — 2D table.

Edge Cases & Corner Traps Handled

  • All identical characters "aaaa": returns 2^N - 1.
  • All distinct characters: returns N.

Count Palindromic Subsequences

Hard
Given a string `s` of lowercase characters, find the number of palindromic subsequences (need not be distinct). Return the answer modulo `10^9 + 7`.
Example Scenarios
1Example 1
Input: s = "abcd"
Output: 4
Explanation:

Combining the input according to Count Palindromic Subsequences logic yields 4.

2Example 2
Input: s = "aab"
Output: 4
Explanation:

Combining the input according to Count Palindromic Subsequences logic yields 4.

3Example 3
Input: s = "aaaa"
Output: 15
Explanation:

Combining the input according to Count Palindromic Subsequences logic yields 15.

Editor
Loading Editor...
s =
"abcd"
Output:Click "Run" above to execute and verify your code here.
4