Created
August 3, 2023 23:08
-
-
Save koalicioous/54a086fb50e79281ba4979689bad48dc to your computer and use it in GitHub Desktop.
Valid Parentheses Solution
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
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