Backtracking
99 / 170BacktrackingLesson 6 of 10About 43 min
The difference between generating everything and filtering, and generating only the valid ones.
The obvious approach to a constrained generation problem is to generate every candidate and keep the valid ones. It is correct and it does an enormous amount of pointless work.
For strings of length n over two symbols that is 2 to the n candidates regardless of how few are valid. When the valid ones are a small fraction, almost all of the search is wasted.
You are generating balanced bracket strings with exactly n pairs, tracking opened and closed. Which pair of conditions allows exactly the valid strings and nothing else?
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.