20. Valid Parentheses
Tags:
Easy
Skills:
Stack
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Solution - stack
SC: O(n)
1function isValid(s: string): boolean {
2 let stack: string[] = []
3 let map = {
4 ')': '(',
5 '}': '{',
6 ']': '['
7 }
8
9 for (const char of s) {
10 if (map[char]) {
11 if (stack.length === 0 || stack.at(-1) !== map[char]) {
12 return false;
13 }
14 stack.pop()
15 } else {
16 stack.push(char)
17 }
18 }
19
20 return stack.length === 0 ? true : false
21};