Reject impossible prefixes early
Use local validity rules for balanced parentheses and nonattacking queens.
Understand the idea
Balanced parentheses maintain the numbers opened and closed. Add an opening bracket while opens < n; add a closing bracket only while closes < opens. For queens, place one queen per row while tracking occupied columns and the two diagonal families.
Trace a small example
With n = 2 pairs, the valid strings are (()) and ()(). A prefix beginning with a closing bracket cannot be repaired later. On a board, queens at (row, col) share a diagonal when row − col or row + col matches; offset row − col for nonnegative table indices.
Conditions and cost
Undo each occupied marker after returning from a queen choice. The pruning rule prevents invalid prefixes, but the search still branches. A single valid arrangement proves existence, not the number of arrangements.
When may a parentheses generator append a closing bracket?
Turn the idea into code
How many balanced strings use three pairs? List them.
Give me a hint
Try each legal first continuation and keep the prefix rule.
Show the explained solution
There are 5: ((())), (()()), (())(), ()(()), and ()()(). Each has equal final counts and no prefix with more closes than opens.
Your notes
Mark complete when you can explain it yourself.