Strings are usually ordered in lexicographical order. That means they are ordered by comparing their leftmost different characters. For example, because . Also because . If one string is an exact prefix of the other it is lexicographically smaller, e.g., .
Given an array of strings sorted in lexicographical order, print all of its permutations in strict lexicographical order. If two permutations look the same, only print one of them. See the 'note' below for an example.
Complete the function next_permutation which generates the permutations in the described order.
For example, . The six permutations in correct order are:
Note: There may be two or more of the same string as elements of .
For example, . Only one instance of a permutation where all elements match should be printed. In other words, if , then print either or but not both.
A three element array having three distinct elements has six permutations as shown above. In this case, there are three matching pairs of permutations where and are switched. We only print the three visibly unique permutations:
The first line of each test file contains a single integer , the length of the string array .
Each of the next lines contains a string .
contains only lowercase English letters.
Print each permutation as a list of space-separated strings on a single line.
Sample Input 0
Sample Output 0
ab cdcd ab
Sample Input 1
Sample Output 1
a bc bcbc a bcbc bc a
This is similar to the note above. Only three of the six permutations are printed to avoid redundancy in output.