Ninja has been given a string ‘STR’ and an integer ‘D’. Ninja has to rearrange the string ‘STR’ such that the same characters are present at least ‘D’ distance from each other.
If it is possible to rearrange the string ‘STR’, then return true; otherwise, print false.
Note :
The string ‘STR’ contains only lowercase alphabet characters.
For example :
If ‘STR’ = “aaadbbcc” and ‘D’ = 2.
Then one of the possible solution is “abacabcd”.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain two single space-separated integers ‘N’ and ‘D’ which denote the size of the ‘STR’ and the distance such that the same characters are present at least ‘D’ distance from each other, respectively.
The next line of each test case contains the string ‘STR’.
Output Format :
For each test case, print "true" if it is possible to rearrange the string ‘STR’, otherwise, print "false" without quotes.
Output for every test case will be printed in a separate line.
Note :
You don’t need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 50
1 <= ‘N’ <= 10000
1 <= ‘D’ <= 26
‘STR’ = {a - z}
Time limit: 1 sec
2
6 2
abcdef
9 2
aaaabbbcc
true
true
In the first test case, all characters are different so any permutation of this string is our answer i,e “abcdef”, “abcdfe”, and so on. As it is possible to rearrange the given string as described in the problem, we return true.
In the second test case, the possible answer is “abcabcaba”.
2
4 3
aabb
6 2
aabbcc
false
true
In the first test case, it is not possible to rearrange the string ‘STR’. So we return false.
In the second test case, one of the possible answers is “abcabc”, ”bacabc” and so on. As it is possible to rearrange the given string as described in the problem, we return true.
Can you think of storing the characters with their freq in a heap?
The basic idea is to first store all the characters of this ‘STR’ in a HashMap. Then iterate over this HashMap, store these key-value pairs in a max-heap and sort according to the frequency of these characters.
Then Iterate over this heap and greedily remove the top different ‘D’ characters from the heap and store them in a resultant string.
The steps are as follows:
O(N), where ‘N’ is the length of the string ‘STR’.
Because first, we are adding all the characters of the ‘STR’ into HashMap that takes O(N) time. Then we are iterating over the HashMap and storing all key-value pairs into a min-heap. As the unique characters will go up to only 26 and so, this takes constant work mainly. Then we are iterating over max-heap and storing all characters into a string ‘ANS’ that also takes O(N) time. So, the overall time complexity is O(N).
O(N), where ‘N’ is the length of the string ‘STR’.
Because first, we are adding all the characters of the ‘STR’ into HashMap. Then we are iterating over the HashMap and storing all key-value pairs into a max-heap. Then we are iterating over the max-heap and storing all characters into a string ‘ANS’. so, the overall space complexity will be O(N).