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
+ * ์์ด๋์ด
3
+ * stack ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํด ์ฌ๋ ๊ดํธ๊ฐ ๋์ค๋ฉด ์์๋๋ก ์ ์ฅํด๋๋ค.
4
+ * stack์ ์ฌ์ฉํ๋ ์ด์ ๋ ๊ฐ์ฅ ์ต๊ทผ ์ฌ๋ ๊ดํธ๊ฐ ์ด๋ค ๊ฒ์ธ์ง ํ์ธํ๊ธฐ ์ํจ์ด๋ค.(๋ง์ง๋ง์ ์ฝ์
ํ ๊ฐ์ ๋จผ์ ์ฌ์ฉํจ)
5
+ * ๋ซ๋ ๊ดํธ๊ฐ ๋์ค๋ฉด stack์ ๋ง์ง๋ง ๊ฐ์ด pair์ธ ์ฌ๋ ๊ดํธ์ธ์ง ์ฒดํฌํ๋ค. ๋ค๋ฅด๋ฉด return false
6
+ * ๋ฌธ์์ด ๋ฐ๋ณต๋ฌธ์ ๋ค ๋๊ณ stack์ ์ฌ๋ ๊ดํธ๊ฐ ๋จ์์์ง ์์์ง ๋ณธ๋ค. ๋จ์์์ผ๋ฉด return false
7
+ * ์์ ๋ ๊ฒฝ์ฐ์ ํด๋น๋์ง ์์ผ๋ฉด return true
8
+ */
9
+ function isValid ( s : string ) : boolean {
10
+ const charSet = new Set ( [ "(" , "{" , "[" ] ) ;
11
+ const openCharStack = [ ] ;
12
+ const CHAR_PAIR = {
13
+ "]" : "[" ,
14
+ "}" : "{" ,
15
+ ")" : "(" ,
16
+ } ;
17
+
18
+ for ( let i = 0 ; i <= s . length - 1 ; i ++ ) {
19
+ const char = s [ i ] ;
20
+ if ( charSet . has ( char ) ) {
21
+ openCharStack . push ( char ) ;
22
+ continue ;
23
+ }
24
+
25
+ if ( char in CHAR_PAIR ) {
26
+ const pair = CHAR_PAIR [ char ] ;
27
+ const lastOpenChar = openCharStack . pop ( ) ;
28
+ if ( lastOpenChar !== pair ) {
29
+ return false ;
30
+ }
31
+ }
32
+ }
33
+
34
+ const isEmpty = openCharStack . length === 0 ;
35
+ return isEmpty ;
36
+ }
37
+ // TC: O(n)
38
+ // SC: O(n)
You canโt perform that action at this time.
0 commit comments