Decode Ways LeetCode solution in JavaScript
1var numDecodings = function (s) {2let dp = Array(s.length).fill(0); // dp[i] means, the total ways of decode for substring up to i3dp[0] = s[0] !== "0" ? 1 : 0;45for (let i = 1; i < s.length; i++) {6//case17if (s[i] !== "0") {8dp[i] = dp[i - 1];9}10//case211if (s[i - 1] === "1" || (s[i - 1] === "2" && parseInt(s[i]) <= 6)) {12dp[i] += dp[i - 2] ?? 1;13}14}1516return dp[s.length - 1];17};