You are given a string S, which is formed by characters βAβ, βBβ and βCβ. You need to count the number of Power Subsequences of the given string S.
Note:
1. A subsequence of string S is a string which we get after removal of zero or more characters from S, and without changing the order of character in the remaining string.
2. A subsequence Sβ of string S, is known as Power Subsequence if Sβ can be represented in the form βAβ^i + βBβ^j + βCβ^k where i, j and k are strictly greater than 0, ie. Sβ can be written as a concatenation of I βAβs, followed by j βBβs, followed by k βCβs.
3. Eg. βABCβ is a Power Subsequence with i=j=k=1, while βBACβ or βABβ or βBCβ are not Power Subsequences.
4. Two subsequences are considered different if and only if the set of indexes of string S, chosen for two subsequences, are different.
The first line of the input contains an integer T denoting the number of test cases.
The first and only line of each test case consists of a string S.
Output format:
For each test case, print an integer that denotes the count of the number of Power Subsequences of the given string S in a separate line.
As the number of Power Subsequences can be large that's why print it modulo 10^9 +7.
Note:
You don't have to print anything, it has already been taken care of. Just Implement the given function.
1 <= T <= 100
1 <= |S| <= 10^4
Were |S| denotes the length of given string S.
Time Limit: 1 sec
1
ABBC
3
There are 3 power subsequences for a given input string: ABC, ABC and ABBC.
1
AABBC
9
There are 9 power subsequences for a given input string: ABC, ABC, ABC, ABC, ABBC, ABBC, AABC, AABC, AABBC.
Generate all possible subsequences of string S and count the power subsequences.
Eg. for string βABC '', let's assume solve is the name of our recursive function.
solve(βABCβ, 0, ββ)
solve(βABCβ, 1, ββ)
solve(βABCβ, 2, ββ)
solve(βABCβ, 3, ββ)
Return 0
solve(βABCβ, 3, βCβ)
Return 0
Return 0
solve(βABCβ, 2, βBβ)
solve(βABCβ, 3, βBβ)
Return 0
solve(βABCβ, 3, βBCβ)
Return 0
Return 0
Return 0
solve(βABCβ, 1, βAβ)
solve(βABCβ, 2, βAβ)
solve(βABCβ, 3, βAβ)
Return 0
solve(βABCβ, 3, βACβ)
Return 0
Return 0
solve(βABCβ, 2, βABβ)
solve(βABCβ, 3, βABβ)
Return 0
solve(βABCβ, 3, βABCβ)
Return 1 // Because ABC is a power subsequence.
Return 1
Return 1
Return 1
This way we get 1 as the count of power subsequences.
O((2^N) * N), where N is the length of the given string S.
As there are 2^N possible subsequences for a string of length N and checking if the subsequence is a power sequence or not, takes an additional O(N) order of time for each subsequence.
O(N), where N is the length of a given string S.
As we are using recursion for generating subsequence and this will use O(N) stack memory.