Skip to content

Instantly share code, notes, and snippets.

@koalicioous
Created August 3, 2023 23:08
Show Gist options
  • Save koalicioous/54a086fb50e79281ba4979689bad48dc to your computer and use it in GitHub Desktop.
Save koalicioous/54a086fb50e79281ba4979689bad48dc to your computer and use it in GitHub Desktop.
Valid Parentheses Solution
function isValid(s: string): boolean {
const stack: string[] = []
const pairs: Record<string,string> = {
')' : '(',
'}' : '{',
']' : '['
}
for (let i = 0; i < s.length;i++) {
if (pairs[s[i]] === undefined) {
stack.push(s[i])
} else {
if (stack.length > 0 && stack[stack.length - 1] === pairs[s[i]]) {
stack.pop()
} else return false
}
}
return stack.length === 0
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment