Last active
May 12, 2020 19:46
-
-
Save remi-bruguier/4c89441cd7690c874676cb1983d0cb57 to your computer and use it in GitHub Desktop.
validParentheses
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
/** | |
* 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