File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
InterviewPrograms/src/com/hackerrank Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments