From 6232f614b60a26aef3c861867a8e0754eb3e415d Mon Sep 17 00:00:00 2001 From: Thiago Lima Date: Fri, 15 Oct 2021 16:46:42 -0300 Subject: [PATCH] feat: add valid-parenthesis challenge solution written in js --- .../20 - validparenthesis/valid-parenthesis.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 leetcode/20 - validparenthesis/valid-parenthesis.js 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