From 155fb243882b55e162021eb58dd9b284d3f196f5 Mon Sep 17 00:00:00 2001 From: yuhyeon99 Date: Mon, 15 Dec 2025 16:56:40 +0900 Subject: [PATCH] valid-parentheses solution --- valid-parentheses/yuhyeon99.js | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 valid-parentheses/yuhyeon99.js diff --git a/valid-parentheses/yuhyeon99.js b/valid-parentheses/yuhyeon99.js new file mode 100644 index 000000000..28b7bd910 --- /dev/null +++ b/valid-parentheses/yuhyeon99.js @@ -0,0 +1,38 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function(s) { + var stack = []; + + var openDic = { + '{': '}', + '[': ']', + '(': ')' + }; + var closeDic = { + '}': '{', + ']': '[', + ')': '(' + }; + + for(let e of [...s]) { + if(!stack.length) { + stack.push(e); + continue; + }; + + const lastEle = stack.pop(); + + if(closeDic[e]) { + if(!openDic[lastEle]) return false; + if(openDic[lastEle] !== e) return false; + continue; + } else { + stack.push(lastEle); + stack.push(e); + } + } + + return stack.length === 0; +};