Coders Crushby Napplied AI
Back to DSA Problems
MediumBacktracking

Generate Parentheses

Generate valid parentheses combinations

Solution Approach

Backtracking with constraints: open < n and close < open. Build valid combinations.

Implementation
def generateParentheses(n):
    result = []
    
    def backtrack(open_count, close_count, current):
        if len(current) == 2 * n:
            result.append(current)
            return
        
        if open_count < n:
            backtrack(open_count + 1, close_count, current + "(")
        
        if close_count < open_count:
            backtrack(open_count, close_count + 1, current + ")")
    
    backtrack(0, 0, "")
    return result
Complexity Analysis

Time Complexity

O(4^n/√n)

Space Complexity

O(n)
Complexity
Time:O(4^n/√n)
Space:O(n)
Asked at
GoogleAmazonFacebook
Coders Crushby Napplied AI

The ultimate interview preparation platform. Master System Design, DSA, and tackle community challenges to crush your FAANG interviews.

Looking for jobs? Visit Napplied AI Jobs Search Agent

System Design

  • All Problems
  • Easy
  • Hard

DSA

  • All Problems
  • Dynamic Programming
  • Graphs

More

  • Problems Arena
  • Growth Paths
  • My Crush

Coders Crush by Napplied AI - Tech Interview & Coding Should Be Effortless