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