You are given a string 'S'. A string is called a split string if it follows the conditions define below.
1. It can be divided into 'K' contiguous substrings such that the first letter of these substrings is the same for all the substrings.
2. It can be divided into 'K' contiguous substrings such that the first letter of these substrings is distinct for all the substrings.
Return 1 if 'S' is a split string otherwise return 0.
For Example:-Let 'N' = 6, 'K' = 2, 'S' = "pppdpa".
It can be divided into "pp" and "pdpa", the first letter of the strings is same.
It can be divided into "ppp" and "dpa", the first letter of the strings is distinct.
So our answer is 1.
First-line contains an integer 'T', which denotes the number of test cases.
For every test case:-
First-line contains two integers 'N' and 'K'.
Second-line contains a String 'S', which consists of 'N' lowercase English letters.
Output Format:-
For each test case, Return 1 if 'S' is a split string otherwise return 0.
Note:-
You don’t need to print anything. Just implement the given function.
1 <= 'T' <= 10
1 <= 'N' <= 10^5
'S' consists of 'N' lowercase English letters.
The Sum of 'N' overall test cases does not exceed 10^5.
Time Limit: 1 sec
2
3 4
xyz
5 3
abaca
0
1
First test case:-
It can be proven that the string 'S' cannot follow the two conditions.
So our answer is 0.
Second test case:-
It can be divided into "ab", "ac", and "a", the first letter of the strings is the same.
It can be divided into "a", "ba", and "ca", the first letter of the strings is distinct.
So our answer is 1.
2
4 1
abcb
5 3
pqpsr
1
0
Can maintaining the frequency of letters in the string help ?
Approach:-
Algorithm:-
O(N), where 'N' is the length of the String 'S'.
We are traversing the String in a linear fashion and the complexity associated with this loop is O(N). Hence, the overall time complexity is of the order O(N).
O(1).
We are storing the occurrence of 26 characters in the hashmap, So the Space Complexity is O(1).