Problem. Given n, generate all combinations of n pairs of parentheses that are well-formed (every ) closes an
earlier unmatched ().
n = 1 → ()
n = 2 → (()) ()()
n = 3 → ((())) (()()) (())() ()(()) ()()()
Lesson 6.2 checked whether a string is valid. Here we generate only valid strings, and the check becomes a rule that decides, at every step, which characters may come next.
ML context. That rule is exactly what constrained decoding does: when a model must produce valid JSON, a well-formed tool call or balanced code, the decoder keeps a small state (here, two counters; in general a stack or a grammar) and masks out every token that would make the output invalid. Change B builds that mask.