Skip to content

Instantly share code, notes, and snippets.

@esco
Created March 12, 2021 03:05
Show Gist options
  • Save esco/5a3aaeb63be3272a41643a8777607809 to your computer and use it in GitHub Desktop.
Save esco/5a3aaeb63be3272a41643a8777607809 to your computer and use it in GitHub Desktop.
// https://leetcode.com/problems/valid-parentheses/solution/
/**
* @param {string} s
* @return {boolean}
*/
function isValid(s) {
const stack = []
const open = {
"(": true,
"{": true,
"[": true
}
const corresponding = {
")": "(",
"}": "{",
"]": "["
}
for (let i = 0; i < s.length; i++) {
if (open[s[i]]) {
stack.push(s[i])
} else if (stack.length == 0) {
return false
} else if (corresponding[s[i]] == stack[stack.length - 1]) {
stack.pop()
} else {
return false
}
}
return stack.length == 0
};
// is this an open?
/*
Edge Case - Check the length of the string / Can
1. Create a stack variable
2. Create hash with characters
3. For loop - iterate through string
4. Push open char into stack (if you encounter opening char)
5. Pop the opening char off the stack if the corresponding char in the string
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment