diff --git a/leetcode/20 - validparenthesis/valid-parenthesis.js b/leetcode/20 - validparenthesis/valid-parenthesis.js new file mode 100644 index 0000000..ab0dcef --- /dev/null +++ b/leetcode/20 - validparenthesis/valid-parenthesis.js @@ -0,0 +1,15 @@ +const isValid = string => { + const pairs = {'(': ')', '[': ']', '{': '}'} + let stack = [] + + for (const char of string) { + const lastChar = stack[stack.length - 1] + + if (char === pairs[lastChar]) + stack.pop() + else + stack.push(char) + } + + return stack.length === 0 +} \ No newline at end of file