File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ function numDecodings ( s : string ) : number {
2
+ // ๋น ๋ฌธ์์ด์ด๊ฑฐ๋ 0์ผ๋ก ์์ํ๋ฉด ๋์ฝ๋ฉ ๋ถ๊ฐ
3
+ if ( ! s || s [ 0 ] === '0' ) return 0 ;
4
+
5
+ const n = s . length ;
6
+
7
+ // ๋ฌธ์์ด ๊ธธ์ด๊ฐ 1์ด๋ฉด ๋ฐ๋ก ๊ฒฐ๊ณผ ๋ฐํ
8
+ if ( n === 1 ) return 1 ;
9
+
10
+ // ์ด๊ธฐ ์ํ
11
+ let prev = 1 ; // dp[0]
12
+ let curr = s [ 0 ] === '0' ? 0 : 1 ; // dp[1]
13
+
14
+ for ( let i = 2 ; i <= n ; i ++ ) {
15
+ let temp = 0 ;
16
+ const oneDigit = parseInt ( s [ i - 1 ] ) ;
17
+ const twoDigit = parseInt ( s [ i - 2 ] + s [ i - 1 ] ) ;
18
+
19
+ // ํ ์๋ฆฌ ์ซ์๋ก ๋์ฝ๋ฉ (1-9)
20
+ if ( oneDigit >= 1 ) {
21
+ temp += curr ;
22
+ }
23
+
24
+ // ๋ ์๋ฆฌ ์ซ์๋ก ๋์ฝ๋ฉ (10-26)
25
+ if ( twoDigit >= 10 && twoDigit <= 26 ) {
26
+ temp += prev ;
27
+ }
28
+
29
+ // ์ํ ์
๋ฐ์ดํธ
30
+ prev = curr ;
31
+ curr = temp ;
32
+ }
33
+
34
+ return curr ;
35
+ }
You canโt perform that action at this time.
0 commit comments