Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

n =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Backtrack while maintaining validity: you can never close more than you’ve opened.

Approach

Build the string character by character.

  • Add ( if opened < n.
  • Add ) if closed < opened. When both counts reach n, record the result.

Why it works

The constraints ensure every prefix is valid, so no invalid strings are generated.

Code
Loading...
Complexity
Time: O(Cn)
Space: O(n)
Solution
Updated: 2026-02-23

Idea

Backtrack while maintaining validity: you can never close more than you’ve opened.

Approach

Build the string character by character.

  • Add ( if opened < n.
  • Add ) if closed < opened. When both counts reach n, record the result.

Why it works

The constraints ensure every prefix is valid, so no invalid strings are generated.

Code
Loading...
Complexity
Time: O(Cn)
Space: O(n)