Backtrack while maintaining validity: you can never close more than you’ve opened.
Build the string character by character.
( if opened < n.) if closed < opened.
When both counts reach n, record the result.The constraints ensure every prefix is valid, so no invalid strings are generated.
Backtrack while maintaining validity: you can never close more than you’ve opened.
Build the string character by character.
( if opened < n.) if closed < opened.
When both counts reach n, record the result.The constraints ensure every prefix is valid, so no invalid strings are generated.