Tutorialspoint
Problem
Solution
Submissions

All Valid Parentheses Combinations

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Python program to generate all valid combinations of parentheses for a given number N pairs. A valid combination means that every opening parenthesis has a corresponding closing parenthesis in the correct order. For example, for N=2, valid combinations are ["(())", "()()"]. The program should return all possible valid combinations as a list of strings.

Example 1
  • Input: n = 3
  • Output: ["((()))", "(()())", "(())()", "()(())", "()()()"]
  • Explanation:
    Step 1: For n=3, we need to generate all valid combinations using 3 opening and 3 closing parentheses.
    Step 2: Each valid combination must have exactly 3 '(' and 3 ')' characters.
    Step 3: At any point while reading from left to right, the count of ')' should not exceed the count of '('.
    Step 4: The final result contains 5 valid combinations for n=3.
Example 2
  • Input: n = 2
  • Output: ["(())", "()()"]
  • Explanation:
    Step 1: For n=2, we need to generate combinations using 2 opening and 2 closing parentheses.
    Step 2: "(())" is valid because the inner parentheses are properly nested within the outer ones.
    Step 3: "()()" is valid because it represents two separate pairs of parentheses in sequence.
    Step 4: ")(" would be invalid because it starts with a closing parenthesis before any opening one.
Constraints
  • 1 ≤ n ≤ 8
  • The output should contain all unique valid combinations
  • Each combination should be a string of length 2*n
  • Time Complexity: O(4^n / √n) - Catalan number complexity
  • Space Complexity: O(4^n / √n) for storing all valid combinations
StringsBacktracking KPMGSamsung
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use backtracking approach to generate all possible combinations
  • Keep track of the count of opening and closing parentheses used so far
  • Add an opening parenthesis if the count of opening parentheses is less than n
  • Add a closing parenthesis if the count of closing parentheses is less than opening parentheses count
  • When the total length reaches 2*n, add the current combination to the result
  • Use recursion to explore all possible paths and backtrack when necessary
  • Ensure that at any point, closing parentheses count never exceeds opening parentheses count

Steps to solve by this approach:

 Step 1: Initialize an empty result list to store all valid combinations and define a recursive backtracking function.
 Step 2: In the backtracking function, maintain current string being built and counts of opening and closing parentheses used.
 Step 3: Check base case - if current string length equals 2*n, add it to result and return.
 Step 4: If opening parentheses count is less than n, recursively add an opening parenthesis and increment open count.
 Step 5: If closing parentheses count is less than opening count, recursively add a closing parenthesis and increment close count.
 Step 6: The recursive calls automatically handle backtracking by exploring all valid paths through the decision tree.
 Step 7: Return the final result list containing all valid parentheses combinations.

Submitted Code :