diff --git a/valid-parentheses/hyejjun.js b/valid-parentheses/hyejjun.js new file mode 100644 index 000000000..0c5f6c311 --- /dev/null +++ b/valid-parentheses/hyejjun.js @@ -0,0 +1,26 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function (s) { + const stack = []; + const map = { + ')': '(', + '}': '{', + ']': '[' + }; + + for (let i = 0; i < s.length; i++) { + const char = s[i]; + + if (char === '(' || char === '{' || char === '[') { + stack.push(char); + } else { + if (stack.length === 0 || stack.pop() !== map[char]) { + return false; + } + } + } + + return stack.length === 0; +};