Skip to content

Instantly share code, notes, and snippets.

@beelarr
Created June 22, 2019 20:57
Show Gist options
  • Save beelarr/d21cf8129f069080b40ea9d2c8783d5c to your computer and use it in GitHub Desktop.
Save beelarr/d21cf8129f069080b40ea9d2c8783d5c to your computer and use it in GitHub Desktop.
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