File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ function numDecodings ( s : string ) : number {
2
+ // SC: O(N)
3
+ const memo : { [ key : number ] : number } = { [ s . length ] : 1 } ;
4
+
5
+ // TC: O(N)
6
+ const dfs = ( start : number ) : number => {
7
+ if ( start in memo ) {
8
+ return memo [ start ] ;
9
+ }
10
+
11
+ if ( s [ start ] === "0" ) {
12
+ // 0์ผ๋ก ์์ํ๋ ๊ฒฝ์ฐ ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์๊ฐ ์์
13
+ memo [ start ] = 0 ;
14
+ } else if (
15
+ start + 1 < s . length &&
16
+ parseInt ( s . substring ( start , start + 2 ) ) < 27
17
+ ) {
18
+ // ๋ค์์ ์ค๋ ๊ธ์๊ฐ ๋๊ธ์ ์ด์ ์๊ณ , start start+1 ๋๊ธ์๊ฐ 1~26 ์ฌ์ด์ ๊ฐ์ธ ๊ฒฝ์ฐ
19
+ memo [ start ] = dfs ( start + 1 ) + dfs ( start + 2 ) ;
20
+ } else {
21
+ // 1๊ธ์๋ง ๋จ์ ๊ฒฝ์ฐ or ์ฒซ ๋๊ธ์๊ฐ 27๋ณด๋ค ํฐ ๊ฒฝ์ฐ
22
+ memo [ start ] = dfs ( start + 1 ) ;
23
+ }
24
+
25
+ return memo [ start ] ;
26
+ } ;
27
+
28
+ // SC: ์ฌ๊ทํธ์ถ O(N)
29
+ return dfs ( 0 ) ;
30
+ }
31
+
32
+ // TC: O(N)
33
+ // SC: O(N)
You canโt perform that action at this time.
0 commit comments