Skip to content

Commit 3100a15

Browse files
committed
sadie100: valid parentheses solution
1 parent 86366c4 commit 3100a15

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
์Šคํƒ์„ ๋งŒ๋“ค์–ด s๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ์ง‘์–ด๋„ฃ๊ณ , ํ—ˆ์šฉ๋˜์ง€ ์•Š์€ ๊ด„ํ˜ธ๊ฐ€ ์˜ค๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค
3+
- ํ—ˆ์šฉํ•˜๋Š” ๊ด„ํ˜ธ : ์—ด๋ ค์žˆ๋Š” ๊ด„ํ˜ธ (, {, [ ํ˜น์€ ๋งˆ์ง€๋ง‰ ์›์†Œ์˜ ์ง์ด ๋˜๋Š” ๋‹ซ๋Š”๊ด„ํ˜ธ
4+
5+
์‹œ๊ฐ„๋ณต์žก๋„ : O(N) - N์€ s์˜ length
6+
๊ณต๊ฐ„๋ณต์žก๋„ : O(N) (๊ด„ํ˜ธ ๋ฐฐ์—ด ๋ฐ ๊ด„ํ˜ธ ์Œ)
7+
*/
8+
9+
function isValid(s: string): boolean {
10+
const stack = []
11+
const openBrackets = ['(', '{', '[']
12+
const bracketMap = {
13+
'(': ')',
14+
'{': '}',
15+
'[': ']',
16+
}
17+
18+
for (const char of s) {
19+
if (openBrackets.includes(char)) {
20+
stack.push(char)
21+
continue
22+
}
23+
const last = stack.at(-1)
24+
if (char === bracketMap[last]) {
25+
stack.pop()
26+
continue
27+
}
28+
29+
return false
30+
}
31+
32+
return stack.length === 0
33+
}

0 commit comments

Comments
ย (0)