Skip to content

Commit 867f110

Browse files
committed
All Combinations of Brackets
1 parent 57f73c1 commit 867f110

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.hackerrank;
2+
3+
public class AllCombinationsOfBrackets {
4+
5+
static int count = 0;
6+
public static void main(String[] args) {
7+
brackets(3, 0, "");
8+
bracketsCount(3, 0, "");
9+
System.out.println(count);
10+
}
11+
12+
public static void brackets(int openStack,int closeStack, String s ){
13+
if(openStack == 0 && closeStack == 0)
14+
System.out.println(s);
15+
if(openStack > 0)
16+
brackets(openStack-1, closeStack+1, s+"(");
17+
if( closeStack > 0)
18+
brackets(openStack, closeStack-1, s+")");
19+
}
20+
21+
public static void bracketsCount(int openStack,int closeStack, String s ){
22+
if(openStack == 0 && closeStack == 0)
23+
count++;
24+
if(openStack > 0)
25+
bracketsCount(openStack-1, closeStack+1, s+"(");
26+
if( closeStack > 0)
27+
bracketsCount(openStack, closeStack-1, s+")");
28+
}
29+
}

0 commit comments

Comments
 (0)