File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ // [91] Decode Ways
2+
3+ /**
4+ * [Time Complexity]
5+ * O(n)
6+ *
7+ * [Space Complexity]
8+ * O(n)
9+ */
10+ function numDecodings ( s : string ) : number {
11+ const n = s . length ;
12+ const memo = new Array < number > ( n + 1 ) . fill ( 0 ) ;
13+
14+ // ์ฒซ๋ฒ์งธ ์ซ์๊ฐ 0์ธ ๊ฒฝ์ฐ ๋์ฝ๋ฉ์ด ๋ถ๊ฐ๋ฅํ๋ฏ๋ก ๋ฏธ๋ฆฌ 0 ๋ฐํ
15+ if ( s [ 0 ] === "0" ) return 0 ;
16+
17+ memo [ 0 ] = 1 ; // ๋น ๋ฌธ์์ด
18+ memo [ 1 ] = 1 ; // 0์ด ์๋ ์ฒซ๋ฒ์งธ ์ซ์ ๋์ฝ๋ฉ
19+
20+ for ( let idx = 2 ; idx <= n ; idx ++ ) {
21+ const oneDigit = Number ( s . slice ( idx - 1 , idx ) ) ; // ํ์ฌ ์ซ์
22+ const twoDigits = Number ( s . slice ( idx - 2 , idx ) ) ; // ํ์ฌ ์ซ์ ์์ ์์ ํฉ์นจ
23+
24+ // ๋ณํํ ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ํด์ค๋ค.
25+ if ( oneDigit >= 1 && oneDigit <= 9 ) {
26+ memo [ idx ] += memo [ idx - 1 ] ;
27+ }
28+ if ( twoDigits >= 10 && twoDigits <= 26 ) {
29+ memo [ idx ] += memo [ idx - 2 ] ;
30+ }
31+ }
32+
33+ return memo [ n ] ;
34+ }
You canโt perform that action at this time.
0 commit comments