Skip to content

Commit 955224c

Browse files
committed
valid-parentheses solution
1 parent bf1af76 commit 955224c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

valid-parentheses/krokerdile.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function(s) {
6+
let ch = {};
7+
ch[')'] = '(';
8+
ch['}'] = '{';
9+
ch[']'] = '[';
10+
11+
let list = s.split('');
12+
let stack = [];
13+
14+
list.forEach((ele)=>{
15+
if(stack.length == 0){
16+
stack.push(ele);
17+
}else{
18+
let len = stack.length;
19+
if(stack[len-1] == ch[ele]){
20+
stack.pop();
21+
}else{
22+
stack.push(ele);
23+
}
24+
}
25+
})
26+
if(stack.length == 0){
27+
return true;
28+
}else{
29+
return false;
30+
}
31+
};

0 commit comments

Comments
 (0)