Skip to content

Commit 92929b1

Browse files
Jeehay28Jeehay28
authored andcommitted
Add decode-ways in TypeScript
1 parent 140b3a3 commit 92929b1

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

decode-ways/Jeehay28.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Approach 4:
2+
// Time Complexity: O(n)
3+
// ✅ Space Complexity: O(1)
4+
5+
function numDecodings(s: string): number {
6+
// s: 2 2 6
7+
// dp: ? ? 1
8+
// cur nxt
9+
10+
let next = 0,
11+
current = 1;
12+
13+
for (let start = s.length - 1; start >= 0; start--) {
14+
const temp = current;
15+
16+
if (s[start] === "0") {
17+
// dp[start] = 0
18+
current = 0;
19+
next = temp;
20+
} else if (
21+
start + 1 < s.length &&
22+
parseInt(s.substring(start, start + 2)) < 27
23+
) {
24+
// dp[start] = dp[start + 1] + dp[start + 2]
25+
current = current + next;
26+
next = temp;
27+
} else {
28+
// dp[start] = dp[start + 1]
29+
next = temp;
30+
}
31+
}
32+
return current;
33+
}
34+
35+
36+
// Approach 3: Dynamic Programming
37+
// Time Complexity: O(n)
38+
// Space Complexity: O(n)
39+
40+
// function numDecodings(s: string): number {
41+
// // 12
42+
// // dp 001
43+
// // 211
44+
45+
// // 226
46+
// // dp 0001
47+
// // 3211
48+
49+
// const dp = Array.from({ length: s.length + 1 }, (el) => 0);
50+
// dp[dp.length - 1] = 1;
51+
52+
// for (let start = s.length - 1; start >= 0; start--) {
53+
// if (s[start] === "0") {
54+
// dp[start] = 0;
55+
// } else if (
56+
// start + 1 < s.length &&
57+
// parseInt(s.substring(start, start + 2)) < 27
58+
// ) {
59+
// dp[start] = dp[start + 1] + dp[start + 2];
60+
// } else {
61+
// dp[start] = dp[start + 1];
62+
// }
63+
// }
64+
65+
// return dp[0];
66+
// }
67+
68+
69+
// Approach 2
70+
// ✅ Time Complexity: O(2^n) -> O(n)
71+
// Space Complexity: O(n)
72+
73+
// function numDecodings(s: string): number {
74+
// const memo = new Map<number, number>();
75+
// memo.set(s.length, 1);
76+
77+
// const dfs = (start: number) => {
78+
// if (memo.has(start)) return memo.get(start);
79+
// if (s[start] === "0") {
80+
// memo.set(start, 0);
81+
// } else if (
82+
// start + 1 < s.length &&
83+
// parseInt(s.substring(start, start + 2)) < 27
84+
// ) {
85+
// memo.set(start, dfs(start + 1)! + dfs(start + 2)!);
86+
// } else {
87+
// memo.set(start, dfs(start + 1)!);
88+
// }
89+
// return memo.get(start);
90+
// };
91+
// return dfs(0)!;
92+
// }
93+
94+
95+
// Approach 1
96+
// ❌ Time Limit Exceeded!
97+
// Time Complexity: O(2^n), where n = s.length
98+
// Space Compexity: O(n), due to recursive call stack
99+
// function numDecodings(s: string): number {
100+
// const dfs = (start: number) => {
101+
// if (start === s.length) {
102+
// return 1;
103+
// }
104+
105+
// if (s[start] === "0") {
106+
// return 0;
107+
// }
108+
109+
// if (start + 1 < s.length && parseInt(s.substring(start, start + 2)) < 27) {
110+
// return dfs(start + 1) + dfs(start + 2);
111+
// } else {
112+
// return dfs(start + 1);
113+
// }
114+
// };
115+
116+
// return dfs(0);
117+
// }

0 commit comments

Comments
 (0)