bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 22. Generate Parentheses

22. Generate Parentheses

Leetcode Solutions

Backtracking Approach

  1. Define a helper function backtrack that takes the current string cur, the count of left parentheses left, and the count of right parentheses right.
  2. If the current string's length is 2n, add it to the result list as a valid combination.
  3. If left is less than n, call backtrack with an additional ( and increment left.
  4. If right is less than left, call backtrack with an additional ) and increment right.
  5. Start the backtracking process by calling backtrack with an empty string and counts set to 0.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR