Print all combinations of balanced parentheses

Write java program to generate all possible n pairs of balanced parentheses. 

For example, if n=1
{}
for n=2
{}{}
{{}}

Solution :-:

package com.tutorialsdesk.algos;
public class ParanthesisCombination {
public static void main(String[] args) { 
printParenthesis(3); 
} 
static void printParenthesis(int n){ 
printParenthesis("",n,n); 
}

static void printParenthesis(String s,int open,int close){ 
if(open > close) 
return; 
if(open == 0 && close == 0){ 
System.out.println(s); 
return; 
} 
if(open < 0 || close<0) 
return;

printParenthesis(s + '{',open-1,close); 
printParenthesis(s + '}',open,close-1); 
}
}


Output :-

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

SHARE
    Blogger Comment
    Facebook Comment