Skip to content

Instantly share code, notes, and snippets.

@rakin92
Last active September 5, 2019 03:24
Show Gist options
  • Save rakin92/e486028dcb736b1fb098e1a721feebfd to your computer and use it in GitHub Desktop.
Save rakin92/e486028dcb736b1fb098e1a721feebfd to your computer and use it in GitHub Desktop.
function isBalanced(s) {
let stack = [];
let openPran = {'{': '}', '[': ']', '(':')'};
let closedPran = {'}': true, ']': true, ')':true};
for (let i = 0; i < s.length; i++) {
const char = s[i];
if(openPran[char]){
stack.push(char);
} else if (closedPran[char]){
if(openPran[stack.pop()] !== char){
return false
}
}
}
return stack.length === 0;
}
console.log(isBalanced('{[()]}'));
console.log(isBalanced('{[(])}'));
console.log(isBalanced('{{[[(())]]}}'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment