Skip to content

Commit cc238f0

Browse files
valid parenthesis
1 parent eec318e commit cc238f0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

20-validParenthesis.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const isValid = (str) => {
2+
if (str.length % 2 !== 0) return false;
3+
const stack = [];
4+
const map = {
5+
'(': ')',
6+
'[': ']',
7+
'{': '}',
8+
};
9+
for (let i = 0; i < str.length; i++) {
10+
if (str[i] === '(' || str[i] === '[' || str[i] === '{') {
11+
stack.push(str[i]);
12+
} else {
13+
const last = stack.pop();
14+
console.log('last', last + ' ' + i);
15+
if (str[i] !== map[last]) {
16+
return false;
17+
}
18+
}
19+
}
20+
if (stack.length !== 0) {
21+
return false;
22+
}
23+
return true;
24+
};
25+
26+
console.log(isValid('()[]{}'));

0 commit comments

Comments
 (0)