Permutation generation systematically produces possible orderings or combinations from a set of elements. A phone mnemonic problem motivates the technique: map each numeral to possible letters, then construct every candidate by extending partial results. section 8.3
A practical generator can be developed in two stages:
- Map each phone-number character to its possible letters, producing lists such as [["g", "h", "i"], ["t", "u", "v"]] for "48". section 8.3
- Combine the lists incrementally: start with one-character arrays, append every option from the next position, and retain the resulting candidates. section 8.3
- Handle an empty input by returning one empty candidate, allowing the recursive or iterative construction to proceed without a missing base case. section 8.3
Phone mnemonics
For "48", the mapping produces [g, h, i] and [t, u, v]. Combining them yields candidates such as "gt", "gu", "gv", "ht", and "iv". A dictionary search can later identify candidates containing useful words, but generating the possibilities is the core task. section 8.3
let phoneMapping: [Character: [Character]] = ["1": ["1"], "2": ["a", "b", "c"], "3": ["d", "e", "f"], "4": ["g", "h", "i"], "5": ["j", "k", "l"], "6": ["m", "n", "o"], "7": ["p", "q", "r", "s"], "8": ["t", "u", "v"], "9": ["w", "x", "y", "z"], "0": ["0"]]
The phone-key mapping makes the input representation concrete before the combination algorithm is implemented. section 8.3
Permutation generation supports brute-force testing, including route search and trying every candidate for a short password. Naive methods can become expensive; Heap’s algorithm is identified as a more efficient alternative for large-scale generation. section 8.5
