Skip to content

Commit 155fb24

Browse files
committed
valid-parentheses solution
1 parent 8fd950f commit 155fb24

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

valid-parentheses/yuhyeon99.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function(s) {
6+
var stack = [];
7+
8+
var openDic = {
9+
'{': '}',
10+
'[': ']',
11+
'(': ')'
12+
};
13+
var closeDic = {
14+
'}': '{',
15+
']': '[',
16+
')': '('
17+
};
18+
19+
for(let e of [...s]) {
20+
if(!stack.length) {
21+
stack.push(e);
22+
continue;
23+
};
24+
25+
const lastEle = stack.pop();
26+
27+
if(closeDic[e]) {
28+
if(!openDic[lastEle]) return false;
29+
if(openDic[lastEle] !== e) return false;
30+
continue;
31+
} else {
32+
stack.push(lastEle);
33+
stack.push(e);
34+
}
35+
}
36+
37+
return stack.length === 0;
38+
};

0 commit comments

Comments
 (0)