#K63097. K-letter Permutations
K-letter Permutations
K-letter Permutations
You are given a set \(S\) of \(n\) unique uppercase letters and an integer \(k\) such that \(1 \le k \le n\). Your task is to generate all possible k-letter permutations (ordered arrangements) using the letters from \(S\). The permutations must be printed in lexicographical order.
Note: Even though the term "combination" is sometimes used, here the order of letters matters, meaning that for a test case with \(S = \{A, B, C\}\) and \(k = 2\), the permutation "AB" is considered different from "BA".
For example, if \(k = 2\) and \(S = \{A, B, C\}\), after sorting \(S\) (which remains "ABC"), the possible permutations are:
[ AB, AC, BA, BC, CA, CB ]
Print the results as a single line for each test case, with the individual permutations separated by a single space.
inputFormat
The input is read from standard input (stdin) and has the following format:
T k n letters k n letters ... (T test cases in total)
Where:
T
is the number of test cases.- For each test case,
k
is an integer representing the length of the permutation to generate. n
is an integer indicating the number of letters in the provided set.letters
is a string ofn
unique uppercase letters (not necessarily sorted).
outputFormat
For each test case, output a single line containing all the generated k-letter permutations separated by a single space, in lexicographical order.
## sample1
2 3 ABC
AB AC BA BC CA CB
</p>