Ordering Strings of Varying Length Lexicographically
This problem asks:
Given: A permutation of at most 12 symbols defining an ordered alphabet A and a positive integer n (n≤4).
Return: All strings of length at most n formed from A, ordered lexicographically.
Required reading
None
Restate the problem
The first important thing to realize about this challenge is that the order in which the characters are given at the outset defines how the results are to be sorted. In this challenge, the order of the result set matters, and it’s not alphabetical, it’s lexicographical, meaning that they need to be returned in the order they were given in the first place.
I’m going to get a list of characters in a specific order and an integer. I need to return all the permutations of that string up to the length of the integer in the order that matches the specific order in which they were given.
Solution steps
First, I reversed the order of the input characters and stored them in a list.
Then, I stepped through that list and made every possible permutation of the current character with all the other characters up to length n.
This method returned all possible permutations up to length n in the correct order.
You can see the final version of the code here.