

1. A string ‘A’ is a subsequence of a string ‘B’ if ‘A’ can be obtained from ‘B’ by deleting several (possibly, zero or all) characters.
2. Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
Let the string be: ababc
The subsequence which can be of form “a^p * b^q * c^r” are: “abc” (p = 1, q = 1, r = 1), “abbc” (p = 1, q = 2, r = 1), “aabc” (p = 2, q = 1, r = 1)
So the count is 5 (“abc” can occur thrice).
The first line of the input contains an integer, 'T’, denoting the number of test cases.
The first line of each test case contains a string ‘S’.
For each test case, print the number of non-empty distinct palindromic subsequences modulo 10^9 + 7.
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |S| <= 10^5
S consists of ‘a’, ‘b’, and ‘c’ characters only.
Where |S| denotes the length of ‘S’.
Time limit: 1 sec
The basic idea is to find all the subsequences of the string and check whether it is of a given form or not. For each character of the string we have two choices with us:
Here is the algorithm:
HELPER(‘S’, ‘IDX’, ‘CURR’) (where ‘S’ is the given string, ‘IDX’ is the current index, ‘CURR’ is the current subsequence which is initialized to “”).
isValid(‘CURR’) (where ‘CURR’ is the string to be checked).
The basic idea is to store the occurrences for each character and find the count of subsequences simultaneously. We create three variables (say, ‘aCnt’, ‘bCnt’, and ‘cCnt’) whose functionalities are:
The total number of subsequences will be stored in the ‘cCnt’ as it contains the last character ‘c’.