Skip to content

Instantly share code, notes, and snippets.

@anjanesh
Last active February 6, 2020 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anjanesh/0883738fd0be1b0436fc875cc747c56e to your computer and use it in GitHub Desktop.
Save anjanesh/0883738fd0be1b0436fc875cc747c56e to your computer and use it in GitHub Desktop.
/*
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