Building a list - why is this code inefficient?
It's inefficient because you're doing a lot of work to create a factorial l, iterating over it l times, each time REDOING all the work of sorting and joining each permutation, and then throwing away most of the results!

Here are some suggestions:


You don't need to extend permutations into an array in memory (loose the to_a), a generator is generally more efficient to iterate over.
You don't need to sort and join the permutations again for every length, this is probably the biggest waste of time. Once they've been sorted and joined you can uniqify them then and then create shorter strings from them (which will need uniqifying again but this will be less work).
Finally, if instead you split and sort the input string at the beginning then you can use the combination function to generate only valid combinations for each length.