Count all distinct palindromic subsequences of a string over the letters a to d

Quick Overview

Given a string of up to 1,000 characters drawn from the letters a, b, c and d, count its distinct non-empty palindromic subsequences modulo 1,000,000,007. It tests counting distinct strings without double counting repeated ones, and building a dynamic program that runs in quadratic time.

Count all distinct palindromic subsequences of a string over the letters a to d

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given a string `s` made of the letters `'a'`, `'b'`, `'c'` and `'d'`, return the number of different non-empty palindromic subsequences in `s`. The answer can be very large, so return it modulo ${10^9 + 7}$. A subsequence of a string is obtained by deleting zero or more characters without changing the order of the remaining ones. A sequence is palindromic if it is equal to the sequence reversed. Two subsequences are different when they are different strings. ### Function Signature ```python def count_palindromic_subsequences(s: str) -> int: ``` ### Rules - Count distinct strings. A palindrome that can be formed from several sets of positions counts once. - The empty subsequence is not counted. - Return the count modulo `1_000_000_007`. ### Constraints - `1 <= len(s) <= 1000` - Every character of `s` is `'a'`, `'b'`, `'c'` or `'d'`. ### Examples ```text Input: s = "bccb" Output: 6 ``` The six palindromes are "b", "c", "bb", "cc", "bcb" and "bccb". A string such as "cbc" is not possible because no "b" sits between the two "c"s. ```text Input: s = "abcdabcdabcdabcd" Output: 244 ``` For lengths 1 to 7 there are 4, 4, 16, 16, 60, 36 and 108 distinct palindromes, which sum to 244. No palindrome of length 8 or more fits, because spelling one out needs more than four passes through "abcd". ```text Input: s = "aaaa" Output: 4 ``` The palindromes are "a", "aa", "aaa" and "aaaa". Each can be picked out from several sets of positions, but each string counts once.

Overview: Given a string of up to 1,000 characters drawn from the letters a, b, c and d, count its distinct non-empty palindromic subsequences modulo 1,000,000,007. It tests counting distinct strings without double counting repeated ones, and building a dynamic program that runs in quadratic time.

Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s made of the letters 'a', 'b', 'c' and 'd', return the number of different non-empty palindromic subsequences in s. The answer can be very large, so return it modulo 109+7{10^9 + 7}.

A subsequence of a string is obtained by deleting zero or more characters without changing the order of the remaining ones. A sequence is palindromic if it is equal to the sequence reversed. Two subsequences are different when they are different strings.

Function Signature

def count_palindromic_subsequences(s: str) -> int:

Rules

  • Count distinct strings. A palindrome that can be formed from several sets of positions counts once.
  • The empty subsequence is not counted.
  • Return the count modulo 1_000_000_007 .

Constraints

  • 1 <= len(s) <= 1000
  • Every character of s is 'a' , 'b' , 'c' or 'd' .

Examples

Input: s = "bccb"
Output: 6

The six palindromes are "b", "c", "bb", "cc", "bcb" and "bccb". A string such as "cbc" is not possible because no "b" sits between the two "c"s.

Input: s = "abcdabcdabcdabcd"
Output: 244

For lengths 1 to 7 there are 4, 4, 16, 16, 60, 36 and 108 distinct palindromes, which sum to 244. No palindrome of length 8 or more fits, because spelling one out needs more than four passes through "abcd".

Input: s = "aaaa"
Output: 4

The palindromes are "a", "aa", "aaa" and "aaaa". Each can be picked out from several sets of positions, but each string counts once.