Created
February 2, 2023 08:06
-
-
Save GeuntaBuwono/50ffc65fa3537078714e7ca48547a1ba to your computer and use it in GitHub Desktop.
LeetCode Valid Parentheses
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
/** | |
* @param {string} s | |
* @return {boolean} | |
*/ | |
var isValid = function(s) { | |
const charStack = [] | |
for (i=0; i < s.length; i++){ | |
let currentChar = s[i]; | |
switch(currentChar) { | |
case '(': charStack.push(')'); | |
break; | |
case '[': charStack.push(']'); | |
break; | |
case '{': charStack.push('}') | |
break; | |
default: | |
if(currentChar !== charStack.pop()) return false | |
} | |
} | |
return charStack.length === 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment