File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ๋ณต์ก๋: O(n) - ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ ํ ๋ฒ์ฉ๋ง ์ํ
3
+ * ๊ณต๊ฐ๋ณต์ก๋: O(n) - ์ต์
์ ๊ฒฝ์ฐ ๋ชจ๋ ๋ฌธ์๊ฐ ์ฌ๋ ๊ดํธ์ผ ๋ ์คํ์ n๊ฐ ์ ์ฅ
4
+ * @param {string } s
5
+ * @return {boolean }
6
+ */
7
+ var isValid = function ( s ) {
8
+ const length = s . length ;
9
+ if ( length % 2 === 1 ) return false ; // ํ์์ผ ๊ฒฝ์ฐ ๋ฐ๋ก return false
10
+
11
+ const MATCHES = { "(" : ")" , "{" : "}" , "[" : "]" } ;
12
+
13
+ // ์ฌ๋ ๊ดํธ๊ฐ ๋์ค๋ฉด ์คํ์ ์ ์ฅ, ๋ซ๋ ๊ดํธ๊ฐ ๋์ค๋ฉด ์คํ์ ๋ง์ง๋ง ์ฌ๋ ๊ดํธ์ ๋น๊ต
14
+ const stack = [ ] ;
15
+ for ( let char of s ) {
16
+ if ( char in MATCHES ) {
17
+ stack . push ( char ) ;
18
+ } else if ( MATCHES [ stack . pop ( ) ] !== char ) {
19
+ return false ;
20
+ }
21
+ }
22
+ // ์ง์ด ๋ง์ผ๋ฉด size 0
23
+ return stack . length === 0 ;
24
+ } ;
You canโt perform that action at this time.
0 commit comments