liveBook · Manning

Permutation generation

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

Permutation generation is the systematic production of possible orderings formed from a set of elements.

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

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Develops a Swift generator incrementally, including mapping, combination, empty input, and performance alternatives, but defers dictionary lookup.
Examples
Demonstrates phone-key mnemonics, route search, and short-password enumeration.
What it assumes
Assumes familiarity with Swift strings, characters, arrays, and backtracking while teaching the combination process from a concrete mapping.
What you can do after
Build it from scratch as a Swift generator that maps positions and appends every available choice.
How it connects
Connects permutation generation with backtracking, the traveling salesman problem, brute-force algorithms, and Heap’s algorithm.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.