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.
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.
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
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: