Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Generate Parentheses Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Generate Parentheses

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a number n denoting n pairs of parentheses, return all valid expressions using the n pairs of parentheses.

Naive Approach

In the naive approach, we can generate all the possible strings with n ‘(’ and n ‘)’ and check if each of them is valid. If they are valid, we can store them in a list.

Analysis

  • Time Complexity: O(4n * n)
  • Space Complexity: O(4n * n)

Optimal Approach

To enumerate this problem, we will try to represent it as a sum of disjoint subsets, so that it is easier to count. Let us define a property of a valid parenthesis sequence as "closure number" as the least index >= 0 so that S[0], S[1], ..., S[2*index+1] is valid. Since every parenthesis sequence has a unique closure number, we can enumerate them individually. Refer to the implementation for better understanding.

Analysis

  • Time Complexity: O(n * nth Catalan Number))
  • Space Complexity: O(n * nth Catalan Number))

Implementation

C++
vector<string> generateParentheses(int n) {
    vector<string> result;
	if(n == 0) {
		string empty = "";
		result.push_back(empty);
	}
	else {
		for(int closure = 0; closure < n; closure++) {
			for(auto left: generateParentheses(closure)) {
				for(auto right: generateParentheses(n - closure - 1)) {
					result.push_back("(" + left + ")" + right);
				}
			}
		}
	}
	return result;
}
Java
class Solution {
	List<String> generateParentheses(int n) {
	    List<String> result = new ArrayList<>();
        if (n == 0) {
			String empty = "";
            result.add(empty);
        } 
		else {
            for (int closure = 0; closure < n; closure++)
                for (String left: generateParentheses(closure))
                    for (String right: generateParentheses(n - closure - 1))
                        result.add("(" + left + ")" + right);
        }
        return result;
	}
}
Related Content
Combination Sum 1
Combination Sum 2
Combinations
Consecutively Descending Integers
Kth Permutation Sequence
Letter Combinations of a Phone Number
N Queens Problem
Palindrome Partitioning
Rat In A Maze
Restore IP Addresses
String Permutations
Subset Sum
Subsets
Subsets - II
Sudoku Solver
Tug of War
Word Break
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet