Skip to content

Commit 15e5d7d

Browse files
authored
Create balanced-paranthesis.js
1 parent b785502 commit 15e5d7d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

balanced-paranthesis.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
*/

0 commit comments

Comments
 (0)