We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 86366c4 commit 3100a15Copy full SHA for 3100a15
1 file changed
โvalid-parentheses/sadie100.tsโ
@@ -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
27
28
29
+ return false
30
31
32
+ return stack.length === 0
33
+}
0 commit comments