File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ const frequency = ( string , character ) => {
2
+ return string . split ( "" ) . reduce ( ( count , c ) => {
3
+ return count + ( c == character ? 1 : 0 ) ;
4
+ } , 0 ) ;
5
+ } ;
6
+
7
+ const validParantheses = ( N , store = "" ) => {
8
+ const openBracketCount = frequency ( store , "(" ) ;
9
+ const closedBracketCount = frequency ( store , ")" ) ;
10
+
11
+ if (
12
+ openBracketCount > N ||
13
+ closedBracketCount > N ||
14
+ closedBracketCount > openBracketCount
15
+ ) {
16
+ return ;
17
+ }
18
+
19
+ if ( store . length === 2 * N ) {
20
+ console . log ( store ) ;
21
+ return ;
22
+ } else {
23
+ validParantheses ( N , store + "(" ) ;
24
+ validParantheses ( N , store + ")" ) ;
25
+ }
26
+ } ;
27
+
28
+ let count = 0 ,
29
+ calls = 0 ;
30
+ console . log ( validParantheses ( 3 , "" ) ) ;
31
+ console . log ( count , calls ) ;
32
+ /*
33
+ Parantheses Conditions
34
+ 1) Open < Count/2
35
+ 2) Closed < Count/2
36
+ 3) Open >= Closed
37
+ 4) Open === Closed return;
38
+ */
You can’t perform that action at this time.
0 commit comments