File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ๊ดํธ ๋ฌธ์์ด ์ ํจ์ฑ ๊ฒ์ฌ
3+ *
4+ * ์คํ ์๋ฃ๊ตฌ์กฐ ํ์ฉ
5+ * 1. ๊ดํธ ์์ ๋งคํํ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์กฐ๊ฑด์ ํ์ธ
6+ * 2. ์ด๋ฆฐ ๊ดํธ๋ฅผ ๋ง๋๋ฉด ํด๋นํ๋ ๋ซํ ๊ดํธ๋ฅผ ์คํ์ ์ง์ push
7+ * 3. ๋ซ๋ ๊ดํธ๋ฅผ ๋ง๋ฌ์ ๋, ์คํ์ด ๋น์ด์๊ฑฐ๋ ์ง์ด ๋ง์ง ์์ผ๋ฉด false
8+ * 4. ๋ฌธ์์ด์ ๋ชจ๋ ์ฒ๋ฆฌํ ํ, ์คํ์ด ๋น์ด์์ด์ผ(๋ฌธ์์ด ๊ธธ์ด๊ฐ 0์ด์ด์ผ) ๋ชจ๋ ๊ดํธ๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ง์ง์ด์ง ๊ฒ(true)
9+ */
10+
11+ /**
12+ * @param {string } s
13+ * @return {boolean }
14+ */
15+ var isValid = function ( s ) {
16+ // ๋น ๋ฌธ์์ด์ด๋ ํ์ ๊ธธ์ด๋ ์ ํจํ์ง ์์
17+ if ( s . length === 0 || s . length % 2 !== 0 ) return false ;
18+
19+ const stack = [ ] ;
20+
21+ for ( let i = 0 ; i < s . length ; i ++ ) {
22+ const char = s [ i ] ;
23+
24+ if ( char === '(' ) {
25+ stack . push ( ')' ) ;
26+ } else if ( char === '{' ) {
27+ stack . push ( '}' ) ;
28+ } else if ( char === '[' ) {
29+ stack . push ( ']' ) ;
30+ } else if ( stack . length === 0 || stack . pop ( ) !== char ) {
31+ // ๋ซ๋ ๊ดํธ๋ฅผ ๋ง๋ฌ์ ๋, ์คํ์ด ๋น์ด์๊ฑฐ๋ ์ง์ด ๋ง์ง ์์
32+ return false ;
33+ }
34+ }
35+
36+ return stack . length === 0 ;
37+ } ;
You canโt perform that action at this time.
0 commit comments