File tree 2 files changed +26
-1
lines changed
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -81,10 +81,11 @@ Roadmap: https://neetcode.io/roadmap
81
81
| 17 | [ Letter Combinations of a Phone Number] ( https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ ) | Medium | [ ts] ( ./TypeScript/17.letter-combinations-of-a-phone-number.ts ) | Backtracking |
82
82
| 70 | [ Climbing Stairs] ( https://leetcode.com/problems/climbing-stairs/description/ ) | Easy | [ ts] ( ./TypeScript/70.climbing-stairs.ts ) | 1-D DP |
83
83
| 746 | [ Min Cost Climbing Stairs] ( https://leetcode.com/problems/min-cost-climbing-stairs/description/ ) | Easy | [ ts] ( ./TypeScript/70.climbing-stairs.ts ) | 1-D DP |
84
- | 198 | [ House Robber] ( https://leetcode.com/problems/house-robber/description/ ) | Medium | [ ts] ( ./TypeScript/70.climbing-stairs .ts ) | 1-D DP |
84
+ | 198 | [ House Robber] ( https://leetcode.com/problems/house-robber/description/ ) | Medium | [ ts] ( ./TypeScript/198.house-robber .ts ) | 1-D DP |
85
85
| 213 | [ House Robber II] ( https://leetcode.com/problems/house-robber-ii/description/ ) | Medium | [ ts] ( ./TypeScript/213.house-robber-ii.ts ) | 1-D DP |
86
86
| 5 | [ Longest Palindromic Substring] ( https://leetcode.com/problems/longest-palindromic-substring/description/ ) | Medium | [ ts] ( ./TypeScript/5.longest-palindromic-substring.ts ) | 1-D DP |
87
87
| 647 | [ Palindromic Substrings] ( https://leetcode.com/problems/palindromic-substrings/description/ ) | Medium | [ ts] ( ./TypeScript/647.palindromic-substrings.ts ) | 1-D DP |
88
+ | 91 | [ Decode Ways] ( https://leetcode.com/problems/decode-ways/description/ ) | Medium | [ ts] ( ./TypeScript/91.decode-ways.ts ) | 1-D DP |
88
89
89
90
### Others
90
91
Original file line number Diff line number Diff line change
1
+ // dp [i] = dp[i+1] + dp[i+2]
2
+ function numDecodings ( s : string ) : number {
3
+ const dp = { [ s . length ] : 1 } ;
4
+
5
+ for ( let i = s . length - 1 ; i >= 0 ; i -- ) {
6
+ if ( s [ i ] === "0" ) {
7
+ dp [ i ] = 0 ;
8
+ } else {
9
+ dp [ i ] = dp [ i + 1 ] ;
10
+ }
11
+
12
+ if (
13
+ i + 1 < s . length &&
14
+ ( s [ i ] === "1" || ( s [ i ] === "2" && "0123456" . includes ( s [ i + 1 ] ) ) )
15
+ ) {
16
+ dp [ i ] = dp [ i ] + dp [ i + 2 ] ;
17
+ }
18
+ }
19
+
20
+ return dp [ 0 ] ;
21
+ }
22
+
23
+ console . log ( numDecodings ( "226" ) ) ;
24
+ console . log ( numDecodings ( "106" ) ) ;
You can’t perform that action at this time.
0 commit comments