Print all combinations of balanced parentheses

3.9

Write a function to generate all possible n pairs of balanced parentheses.

For example,

Input : n=1
Output: {}

Input : n=2
Output: 
{}{}
{{}}

Algorithm:
Keep track of counts of open and close brackets.

  1. Initialize these counts as 0.
  2. Recursively call the _printParenthesis() function until open bracket count is less than the given n.
    • If open bracket count becomes more than the close bracket count, then put a closing bracket and recursively call for the remaining brackets.
    • If open bracket count is less than n, then put an opening bracket and call _printParenthesis() for the remaining brackets.

Thanks to Shekhu for providing the below code.

C/C++

// C program to Print all combinations
// of balanced parentheses
# include<stdio.h>
# define MAX_SIZE 100

void _printParenthesis(int pos, int n, int open, int close);

// Wrapper over _printParenthesis()
void printParenthesis(int n)
{
    if(n > 0)
    	_printParenthesis(0, n, 0, 0);
    return;
}	 

void _printParenthesis(int pos, int n, int open, int close)
{
    static char str[MAX_SIZE];	 
    
    if(close == n) 
    {
    	printf("%s \n", str);
    	return;
    }
    else
    {
    	if(open > close) 
    	{
    		str[pos] = '}';
    		_printParenthesis(pos+1, n, open, close+1);
    	}
    	
    	if(open < n)
    	{
    	str[pos] = '{';
    	_printParenthesis(pos+1, n, open+1, close);
    	}
    }
}

// Driver program to test above functions
int main()
{
    int n = 3;
    printParenthesis(n);
    getchar();
    return 0;
}

Java

// Java program to print all 
// combinations of balanced parentheses
import java.io.*;

class GFG 
{
    // Function that print all combinations of 
    // balanced parentheses
    // open store the count of opening parenthesis
    // close store the count of closing parenthesis
    static void _printParenthesis(char str[], int pos, int n, int open, int close)
    {
        if(close == n) 
        {
            // print the possible combinations
            for(int i=0;i<str.length;i++)
                System.out.print(str[i]);
            System.out.println();
            return;
        }
        else
        {
            if(open > close) {
                str[pos] = '}';
                _printParenthesis(str, pos+1, n, open, close+1);
            }
            if(open < n) {
                str[pos] = '{';
                _printParenthesis(str, pos+1, n, open+1, close);
            }
        }
    }
    
    // Wrapper over _printParenthesis()
    static void printParenthesis(char str[], int n)
    {
        if(n > 0)
        _printParenthesis(str, 0, n, 0, 0);
        return;
    }
    
    // driver program 
	public static void main (String[] args) 
	{
		int n = 3;
		char[] str = new char[100];
		printParenthesis(str, n);
	}
}

// Contributed by Pramod Kumar


Output:

{}{}{}
{}{{}}
{{}}{}
{{}{}}
{{{}}}

Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the same problem.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.9 Average Difficulty : 3.9/5.0
Based on 45 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.