File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ *@link https://leetcode.com/problems/valid-parentheses/description/
3
+ *
4
+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
5
+ * - ๋ซํ ๊ดํธ ๋์์ ๋ ์ด๋ฆฐ ๊ดํธ์ ๋ง์ง๋ง ๊ฐ๊ณผ ์ง์ด ๋ง๋์ง ์ฐพ์์ผ๋๋๊น stack ์ฌ์ฉ
6
+ *
7
+ * ์๊ฐ๋ณต์ก๋ : O(n)
8
+ * - ๋ฌธ์์ด ์ํํ๋ฉด์ ๊ดํธ์ฒดํฌํ๋๊น O(n)
9
+ *
10
+ * ๊ณต๊ฐ๋ณต์ก๋ : O(k)
11
+ * - pairs ๊ฐ์ฒด ๊ณ ์ ๋ ํฌ๊ธฐ๋ก ์ ์ฅ O(1)
12
+ * - stack์ ์ด๋ฆฐ ๊ดํธ ๊ฐ์๋งํผ ๋ด๊ธฐ๋๊น O(k)
13
+ */
14
+
15
+ function isValid ( s : string ) : boolean {
16
+ const pairs : Record < string , string > = {
17
+ ")" : "(" ,
18
+ "}" : "{" ,
19
+ "]" : "[" ,
20
+ } ;
21
+ const stack : string [ ] = [ ] ;
22
+
23
+ for ( const char of s ) {
24
+ // ๋ซ๋ ๊ดํธ ๋์จ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
25
+ if ( pairs [ char ] ) {
26
+ // ์ง์ด ๋ง์ง ์๋ ๊ฒฝ์ฐ
27
+ if ( stack [ stack . length - 1 ] !== pairs [ char ] ) return false ;
28
+
29
+ // ์ง์ด ๋ง๋ ๊ฒฝ์ฐ
30
+ stack . pop ( ) ;
31
+ } else {
32
+ // ์ด๋ฆฐ ๊ดํธ๋ ๋ฐ๋ก stack์ ์ ์ฅ
33
+ stack . push ( char ) ;
34
+ }
35
+ }
36
+
37
+ return stack . length === 0 ;
38
+ }
You canโt perform that action at this time.
0 commit comments