We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4ac5487 commit 91f1443Copy full SHA for 91f1443
decode-ways/tolluset.ts
@@ -0,0 +1,26 @@
1
+/**
2
+ * TC: O(n)
3
+ * SC: O(n)
4
+ */
5
+function numDecodings(s: string): number {
6
+ const n = s.length;
7
+ const dp = new Array(n + 1).fill(0);
8
+
9
+ dp[0] = 1;
10
+ dp[1] = Number(s[0]) === 0 ? 0 : 1;
11
12
+ for (let i = 2; i <= n; i++) {
13
+ const oneDigit = Number(s.slice(i - 1, i));
14
+ const twoDigits = Number(s.slice(i - 2, i));
15
16
+ if (oneDigit >= 1 && oneDigit <= 9) {
17
+ dp[i] += dp[i - 1];
18
+ }
19
20
+ if (twoDigits >= 10 && twoDigits <= 26) {
21
+ dp[i] += dp[i - 2];
22
23
24
25
+ return dp[n];
26
+}
0 commit comments