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 7b3401f commit 6c7ec07Copy full SHA for 6c7ec07
1 file changed
decode-ways/nowrobin.js
@@ -0,0 +1,30 @@
1
+/**
2
+ * @param {string} s
3
+ * @return {number}
4
+ */
5
+var numDecodings = function(s) {
6
+ if (!s || s[0] === '0') {
7
+ return 0;
8
+ }
9
+
10
+ const n = s.length;
11
+ const dp = new Array(n + 1).fill(0);
12
+ dp[0] = 1;
13
+ dp[1] = 1;
14
15
+ for (let i = 2; i <= n; ++i) {
16
+ const oneDigit = parseInt(s[i - 1]);
17
+ const twoDigits = parseInt(s.substring(i - 2, i));
18
19
+ if (oneDigit !== 0) {
20
+ dp[i] += dp[i - 1];
21
22
23
+ if (10 <= twoDigits && twoDigits <= 26) {
24
+ dp[i] += dp[i - 2];
25
26
27
28
+ return dp[n];
29
+};
30
0 commit comments