Skip to content

Commit 8094a7e

Browse files
committed
update: 20
1 parent c8cf955 commit 8094a7e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
2323
| 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [JavaScript](./src/3sum-closest/res.js) | Medium |
2424
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [JavaScript](./src/letter-combinations-of-a-phone-number/res.js) | Medium |
2525
| 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [JavaScript](./src/remove-nth-node-from-end-of-list/res.js) | Medium |
26+
| 20 | [valid-parentheses](https://leetcode.com/problems/valid-parentheses/) | [TypeScript](./src/valid-parentheses/res.ts) | Easy |
2627
| 22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | [JavaScript](./src/generate-parentheses/res.js) | Medium |
2728
| 24 | [swap-nodes-in-pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [TypeScript](./src/swap-nodes-in-pairs/res.ts) | Medium |
2829
| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [JavaScript](./src/remove-duplicates-from-sorted-array/res.js) | Easy |

‎src/valid-parentheses/res.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function isValid(s: string): boolean {
2+
const sLength = s.length;
3+
if (sLength < 2) {
4+
return !sLength;
5+
}
6+
7+
let stack: string[] = [s[0]];
8+
9+
for (let i = 1; i < sLength; i++) {
10+
const current = s[i];
11+
if (['{', '(', '['].includes(current)) {
12+
stack.push(current);
13+
} else if (['}', ')', ']'].includes(current)) {
14+
const matchedList = ['{}', '()', '[]'];
15+
if (matchedList.includes(stack[stack.length-1] + current)) {
16+
stack.pop();
17+
} else if (stack.length) {
18+
break;
19+
} else {
20+
stack.push(current);
21+
}
22+
}
23+
}
24+
25+
return stack.length === 0;
26+
};

0 commit comments

Comments
 (0)