Created
June 22, 2019 20:57
-
-
Save beelarr/d21cf8129f069080b40ea9d2c8783d5c 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
const input = ["[[)}", "{()}"] | |
const balanceBrackets = arr => { | |
const result = arr.map(string => { | |
const stack = []; | |
const map = { | |
'(': ')', | |
'[': ']', | |
'{': '}' | |
} | |
for (let i = 0; i < string.length; i++) { | |
if (string[i] === '(' || string[i] === '{' || string[i] === '[' ) { | |
stack.push(string[i]); | |
} | |
else { | |
if (string[i] !== map[stack.pop()]) return 'NO' | |
} | |
} | |
if (stack.length !== 0) return 'NO' | |
return 'YES'; | |
}) | |
return result | |
} | |
balanceBrackets(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment