Skip to content

Instantly share code, notes, and snippets.

@basekays
Last active June 20, 2018 01:15
Show Gist options
  • Select an option

  • Save basekays/8b59a40225f8ee5185f887b42b2953b4 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/8b59a40225f8ee5185f887b42b2953b4 to your computer and use it in GitHub Desktop.
/**
* @param {string} s
* @return {boolean}
*/
const reference = {
'(': ')',
'{': '}',
'[': ']',
};
function isValid(string) {
const stack = [];
for (let i = 0; i < string.length; i++) {
let target = string[i];
if (reference[target]) {
stack.push(reference[target]);
} else {
if (target != stack.pop()) {
return false;
}
}
}
return stack.length == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment