From e9e3dec62bcf8bc21fb1aa987ee0a52cca20d1f8 Mon Sep 17 00:00:00 2001 From: Mauricio Jr <mjdasilva@alelo.com.br> Date: Fri, 15 Oct 2021 09:05:13 -0300 Subject: [PATCH] feat: add new solution with js --- .../valid-parenthesis.js | 26 +++++++++++++++++++ 1 file changed, 26 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..5aec27c --- /dev/null +++ b/leetcode/20 - validparenthesis/valid-parenthesis.js @@ -0,0 +1,26 @@ +const OBJECT_MAPPING = { + ')': '(', + ']': '[', + '}': '{', +} + +var isValid = function (s) { + const opens = [] + for (c of s) { + if (c === '(' || c === '[' || c === '{') { + opens.push(c) + } else { + if (opens.length === 0) { + return false; + } + const lastOpen = opens[opens.length - 1]; + if (OBJECT_MAPPING[c] != lastOpen) { + return false + } else { + opens.pop(); + } + } + } + + return opens.length === 0 +}; \ No newline at end of file