forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres.ts
26 lines (23 loc) · 705 Bytes
/
res.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function isValid(s: string): boolean {
const sLength = s.length;
if (sLength < 2) {
return !sLength;
}
let stack: string[] = [s[0]];
for (let i = 1; i < sLength; i++) {
const current = s[i];
if (['{', '(', '['].includes(current)) {
stack.push(current);
} else if (['}', ')', ']'].includes(current)) {
const matchedList = ['{}', '()', '[]'];
if (matchedList.includes(stack[stack.length-1] + current)) {
stack.pop();
} else if (stack.length) {
break;
} else {
stack.push(current);
}
}
}
return stack.length === 0;
};