Sunday, April 16, 2017


For a given string formed from letters a,b,and c, find all subsequences of the form a..b...c.. where the a, b, and c can be repeated any number of times homogenously.
subsequences are considered different if the indexes of the subsequences are different
For example:
Input  : abbc
Output : 3
Subsequences are abc, abc and abbc

Input  : abcabc
Output : 7
Subsequences are abc, abc, abbc, aabc
abcc, abc and abc
int GetCountABC(string s)
{
int a = 0;
int ab = 0;
int abc = 0;
for (int i = 0; i <=s.length; i++)
{
if (s[i] = 'a')
    a = 1 + 2*a;
else if (s[i] == 'b')
    ab = a + 2*ab;
else
   abc = ab + 2*abc;
}
return abc;
}
// Alternatively, we could even try to enumerate them based on their occurrence after we separate out the mandatory abc or as many abc as we can form
// untested but only to illustrate the pattern
Int GetCountABC(List<int>Str, int start, int end)
{
If (start>=end-2) return 0;
Var A = Str.Select(x => x == 'a');
Var B = Str.Select(x => x == 'b');
Var C = Str.Select(x => x == 'c');
int count = 1; // for abc
int n  = A.Count + B.Count + C.Count - 3
For (int k = 1; k <=n; k++)
       count += GetNChooseKDP(n, k);
return count;
}

No comments:

Post a Comment