Last active
September 5, 2019 03:24
-
-
Save rakin92/e486028dcb736b1fb098e1a721feebfd 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
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