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.
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.