Ninja has been given a string ‘STR’ consisting of lowercase English letters. ‘STR’ might contain duplicate characters too. Ninja has to return all unique permutations of the given string in lexicographically increasing order.
Can you help Ninjas to do this task?.
String 'STR1' is lexicographically less than string 'STR2', if either 'STR1' is a prefix of 'STR2' (and 'STR1' ≠ 'STR2'), or there exists such i (1 <= i <= min(|'STR1'|, |'STR2'|)), such that 'STR1[i]’ < 'STR[i]’, and for any j (1 <= ‘j’ < ‘i’) 'STR1[i]’ = 'STR2[i]’. Here |'STR1'| denotes the length of the string 'STR1'.
For example :If the string is “aab”, then its unique permutations in lexicographically increasing order are { “aab”, “aba”, “baa” }.
The first line of input contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line and only line of each test case contain a string ‘STR’ consisting of lowercase English letters.
Output Format :
For every test case, the permutations of the given string are printed in lexicographically increasing order separated by space.
The output of each test case is printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 5
1 <= |’STR’| <= 9
Time Limit: 1 second
2
aa
abc
aa
abc acb bac bca cab cba
In the 1st test case, there is only 1 permutation of the given string that is possible i.e { “aa” }.
In the 2nd test case, there are 6 permutations of the given string which are { “abc”, “acb”, “bac”, “bca”, “cab”, “cba” }.
2
bcb
acaa
bbc bcb cbb
aaac aaca acaa caaa
Think of using backtracking to generate all permutations.
Our main task in this problem is to handle duplicates. So we can use an extra boolean array/list, ‘USED’, which indicates whether the value is added in our resultant list/vector ‘PERM_LIST’ or not. First, we sort the given input ‘STR’ to make sure we can skip the same characters. When a character is the same as the previous, we can use it only if the last character is not used.
Approach:
uniquePremHelper(‘STR_ARR’, ’USED’, ‘PREM_LIST’, ‘RES’) function is explained below:
O(N! * N), Where N is the length of the given string ‘STR’.
For any recursive function, the time complexity is O(branches ^ depth) * amount of work at each node in the recursive call tree. In this case, we have N * (N-1) * (N-2) * (N-3) * ... * 1= N! branches at each level ‘N’. So the total recursive calls are O(N!)
We do N-amount of work in each node of the recursive call tree:
(a) the for-loop and (b) at each leaf when we add ‘N’ elements to a ‘PERM_LIST’. So this is a total of O(N) additional work per node.
Therefore, the worst-case time complexity is O(N! * N).
O(N!), Where N is the length of the given string ‘STR’.
O(N) recursion stack is used by the recursive function. We are storing the permutations in a list/vector ‘PERM_LIST’of size ‘N’ which takes O(N) space. We are also using a ‘USED’ array/list of size ‘N’ which indicates whether the ‘STR[i]’ is added in our resultant list/vector ‘PERM_LIST’ or not.
Thus, the final space complexity is O(N).