Skip to content

Instantly share code, notes, and snippets.

@anjanesh
Last active Feb 6, 2020
Embed
What would you like to do?
/*
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