You are given a String S of length N.
Now, a special subsequence is one that can be represented in the form a^i b^j c^k, where
i≥1, j≥1 and k≥1. For example ,if i=2, j=1, k=3, it represents the string aabccc. In short, a special subsequence is a subsequence that first consist of
i ′a′ characters, followed by j ′b′ characters, followed by k ′c′ characters, where
i≥1, j≥1 and k≥1
Now, you need to find the number of special subsequences of String S.
Note: Two subsequences are considered different if the set of array indexes picked for the 2 subsequences are different.
Sample Input:
abcabc
Sample Output:
7
Explanation:
Valid sub sequences are(1-based indexing):
{1,2,3}
{1,2,6}
{1,5,6}
{4,5,6}
{1,2,5,6}
{1,4,5,6}
{1,2,3,6}
My Approach:
My approach is to recursively go through each and every index of string and check whether the resultant substring meets the criteria or not. But, as you can see this approach will grow exponentially with the length of string. Is there any better way to solve this problem?