Problem. Given a corpus as word frequencies, learn M merges:
repeat M times:
count every adjacent symbol pair, weighted by word frequency
pick the most frequent pair (ties: the smallest pair)
merge it everywhere, left to right, and record it
Return the ordered list of merges. Lesson 13.6 did one step efficiently. Training does tens of thousands, so two costs dominate: rewriting the corpus (solved in 13.6: touch only the words containing the pair) and finding the best pair each step, which a scan over all P distinct pairs makes O(P) per merge. A heap fixes the second.
ML context. This is how GPT-style tokenizers are trained, usually on bytes (Change A) so any text can be encoded. Vocabulary size (the number of merges plus 256) is a model design choice: larger vocabularies make sequences shorter but embedding tables bigger. Implementing the trainer is a "hard" ML-round question because it combines a hash map, an inverted index and a priority queue with lazy deletion.