Skip to content

Commit a916604

Browse files
committed
feat: valid parentheses
1 parent eff63cb commit a916604

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: s์˜ ๊ธธ์ด๋งŒํผ ์ˆœํšŒํ•˜๋ฏ€๋กœ, O(n)
3+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: ์Šคํƒ์€ ์ตœ๋Œ€ s์˜ ๊ธธ์ด๋งŒํผ ๋ฌธ์ž๋ฅผ ์ €์žฅํ•˜๋ฏ€๋กœ, O(n)
4+
*/
5+
/**
6+
* @param {string} s
7+
* @return {boolean}
8+
*/
9+
var isValid = function(s) {
10+
const stack = [];
11+
const brackets = {
12+
'(': ')',
13+
'{': '}',
14+
'[': ']'
15+
}
16+
for(const b of s) {
17+
if(b in brackets) {
18+
stack.push(b);
19+
} else {
20+
const cur = stack.pop();
21+
if(brackets[cur] !== b) {
22+
return false;
23+
}
24+
}
25+
}
26+
return stack.length === 0;
27+
};

0 commit comments

Comments
ย (0)