Miscellaneous problems · Exercise 81 of 100
Generate string permutations
For a string of distinct letters, put each unused letter in the next position in turn. After exploring one choice, free that letter for later choices. Trying letters in alphabetical order at every position lists all complete arrangements in alphabetical order.
Try it yourself
List the permutations of INK by trying unused letters alphabetically at each position. Which three-letter string comes immediately after KNI?
One-step hint
Undo the choices in KNI from the right, returning each letter to the pool. Where can you first choose a larger letter and then finish alphabetically?
Worked solution
- With K first, the unused letters I and N give KIN followed by KNI. That finishes the choices starting with K.
- The next first letter is N. Choosing I before K for the second position leaves K for the last, giving NIK.