String Permutation
Time limit5sMemory limit512 MB
Print every permutation of a short string of distinct characters, in the order given by the original character positions.
- Level
Easy3 of 10
- Topics
- Backtracking, Recursion, Implementation
- Solved
- No attempts yet
Problem
Write a recursive function that reads a string and prints all permutations of that string. The string consists of a set of distinct characters.
Input
The first line contains (), the number of test cases. Each of the next lines contains one string. Its length satisfies , and it consists of distinct non-whitespace characters.
Output
For each test case, first print a line in the format Case # x:, where x is the test case number starting from 1. Then print the permutations of the string, one per line.
Print the permutations in this order. Choose the first character from the characters of the input string, taking them in the order they appear in the input, and for each choice apply the same rule recursively to the remaining characters. For example, the permutations of abc are printed in the order abc, acb, bac, bca, cab, cba. The order depends on the positions of the characters in the input, not on alphabetical order.