Skip to content

Instantly share code, notes, and snippets.

@remi-bruguier
Last active May 12, 2020 19:46
Show Gist options
  • Save remi-bruguier/4c89441cd7690c874676cb1983d0cb57 to your computer and use it in GitHub Desktop.
Save remi-bruguier/4c89441cd7690c874676cb1983d0cb57 to your computer and use it in GitHub Desktop.
validParentheses
/**
* Valid Parentheses
* @param {string} s
* @return {boolean}
*/
const validParentheses = (s) => {
if (s === null || !s.length) return true;
const chars = s.split('');
const stack = [];
for (const c of chars) {
if (c === '[') stack.push(']');
else if (c === '{') stack.push('}');
else if (c === '(') stack.push(')');
else if (!stack.length || c !== 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