


If string S is equal to “abadaa”.
Then S can be split in the following ways:
X = “a” and Y = “badaa”, string X has 1 unique character and Y has 3 unique characters.
X = “ab” and Y = “adaa”, string X has 2 unique characters and Y has 2 unique characters.[GOOD SPLIT]
X = “aba” and Y = “daa”, string X has 2 unique characters and Y has 2 unique characters.[GOOD SPLIT]
X = “abad” and Y = “aa”, string X has 3 unique characters and Y has 1 unique character.
X = “abada” and Y = “a”, string X has 3 unique characters and Y has 1 unique character.
Therefore we will return value 2 as we are able to find 2 good splits.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a string ‘S’ denoting the input string.
For each test case, print a single integer denoting the number of good splits.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= S.length <= 200
S contains only lower-case English alphabets
Time limit: 1 sec
We can simply consider all the splits which lead to splitting the original string into two non-empty strings, for each of the strings we can calculate distinct characters in it with the help of a frequency array of size equal to 26 as there are 26 lower-case English alphabets.
We can finally return the number of good splits calculated.
The steps are as follows :
We can simply consider all the splits which lead to splitting the original string into two non-empty strings, we can precompute the number of distinct characters for the prefix substring from 0 to ‘i’ and also precompute the distinct characters in suffix substring from (i+1) to (S.length-1). If the number of distinct characters is equal then we can simply conclude that this split will be good.
We can finally return the number of good splits calculated.
The steps are as follows :