Last active
February 6, 2020 02:00
-
-
Save anjanesh/0883738fd0be1b0436fc875cc747c56e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage : | |
node brackets.js | |
((a+b*d)*(x/2))*(1+(y+(x-2)/10)) | |
[ '(a+b*d)', | |
'(x/2)', | |
'((a+b*d)*(x/2))', | |
'(x-2)', | |
'(y+(x-2)/10)', | |
'(1+(y+(x-2)/10))' ] | |
*/ | |
let s = "((a+b*d)*(x/2))*(1+(y+(x-2)/10))" | |
let opening_bracket_pos = [] | |
let stack = [] | |
for (let i = 0;i < s.length; i++) | |
{ | |
if (s[i] == '(') | |
{ | |
opening_bracket_pos.push(i) | |
} | |
else if (s[i] == ')') | |
{ | |
start_pos = opening_bracket_pos.pop() | |
stack.push(s.substring(start_pos, i + 1)) | |
} | |
} | |
console.log(s); | |
console.log(stack); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment