
The name of the cake should start with 'c', 'a', 'k', 'e', or 's'.
No two cakes should begin with the same letter.
Let 'N' = 4, S = ["choco", "apple", "kesar", "strawberry"].
There exist 4 different ways to choose three cakes following all the required conditions i.e. {"choco", "apple", "kesar"}, {"choco", "apple", "strawberry"}, {"choco", "strawberry", "kesar"}, {"strawberry", "apple", "kesar"}.
The first line contains an integer 'T', which denotes the number of test cases.
For every test case:-
The first line contains only one integer 'N' the number of cakes.
The second line contains 'N' space-separated strings denoting the elements of the 'S'.
For each test case, return the number of ways to choose three cakes irrespective of the order.
You don’t need to print anything. Just implement the given function.
1 <= 'T' <= 10
1 <= 'N' <= 10^4
0 <= |S[i]| <= 10
All the names of cakes are distinct and consist of lowercase letters.
Time Limit: 1 sec
Approach:-
Let's first find the frequency of the number of cakes starting with the letters 'c', 'a', 'k', 'e', or 's' each. Now when choosing 3 cakes, there are no more than 10 patterns of the first character of those names. Let f('x') denote the number of cakes that begin with the character 'x'. The number of ways to choose cakes such that the name begins with 'c', 'a' and 'k' will be f('c') * f('a') * f('k'). So we just need to compute the answer for each of the 10 patterns, this can be achieved using three pointers.
Algorithm:-