int numDecodings(string s) {
function<int(int)> ways = [&](int i) -> int {
if (i == (int)s.size()) return 1;
if (s[i] == '0') return 0;
int total = ways(i + 1);
if (i + 1 < (int)s.size() && stoi(s.substr(i, 2)) <= 26) total += ways(i + 2);
return total;
};
return ways(0);
}
public int numDecodings(String s) {
return ways(s, 0);
}
private int ways(String s, int i) {
if (i == s.length()) return 1;
if (s.charAt(i) == '0') return 0;
int total = ways(s, i + 1);
if (i + 1 < s.length() && Integer.parseInt(s.substring(i, i + 2)) <= 26) total += ways(s, i + 2);
return total;
}
def num_decodings(s):
def ways(i):
if i == len(s): return 1
if s[i] == '0': return 0
total = ways(i + 1)
if i + 1 < len(s) and int(s[i:i + 2]) <= 26: total += ways(i + 2)
return total
return ways(0)
function numDecodings(s: string): number {
const ways = (i: number): number => {
if (i === s.length) return 1;
if (s[i] === '0') return 0;
let total = ways(i + 1);
if (i + 1 < s.length && Number(s.slice(i, i + 2)) <= 26) total += ways(i + 2);
return total;
};
return ways(0);
}
int numDecodings(string s) {
unordered_map<int, int> memo;
function<int(int)> ways = [&](int i) -> int {
if (i == (int)s.size()) return 1;
if (s[i] == '0') return 0;
auto known = memo.find(i);
if (known != memo.end()) return known->second;
int total = ways(i + 1);
if (i + 1 < (int)s.size() && (s[i] == '1' || (s[i] == '2' && s[i + 1] <= '6'))) total += ways(i + 2);
memo[i] = total;
return total;
};
return ways(0);
}
public int numDecodings(String s) {
Map<Integer, Integer> memo = new HashMap<>();
return ways(s, memo, 0);
}
private int ways(String s, Map<Integer, Integer> memo, int i) {
if (i == s.length()) return 1;
if (s.charAt(i) == '0') return 0;
Integer known = memo.get(i);
if (known != null) return known;
int total = ways(s, memo, i + 1);
if (i + 1 < s.length() && (s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) <= '6'))) total += ways(s, memo, i + 2);
memo.put(i, total);
return total;
}
def num_decodings(s):
memo = {}
def ways(i):
if i == len(s): return 1
if s[i] == '0': return 0
known = memo.get(i)
if known is not None: return known
total = ways(i + 1)
if i + 1 < len(s) and (s[i] == '1' or (s[i] == '2' and s[i + 1] <= '6')): total += ways(i + 2)
memo[i] = total
return total
return ways(0)
function numDecodings(s: string): number {
const memo = new Map<number, number>();
const ways = (i: number): number => {
if (i === s.length) return 1;
if (s[i] === '0') return 0;
const known = memo.get(i);
if (known !== undefined) return known;
let total = ways(i + 1);
if (i + 1 < s.length && (s[i] === '1' || (s[i] === '2' && s[i + 1] <= '6'))) total += ways(i + 2);
memo.set(i, total);
return total;
};
return ways(0);
}
int numDecodings(string s) {
int n = s.size();
vector<int> dp(n + 1, 0);
dp[n] = 1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '0') continue;
dp[i] = dp[i + 1];
if (i + 1 < n && (s[i] == '1' || (s[i] == '2' && s[i + 1] <= '6'))) dp[i] += dp[i + 2];
}
return dp[0];
}
public int numDecodings(String s) {
int n = s.length();
int[] dp = new int[n + 1];
dp[n] = 1;
for (int i = n - 1; i >= 0; i--) {
if (s.charAt(i) == '0') continue;
dp[i] = dp[i + 1];
if (i + 1 < n && (s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) <= '6'))) dp[i] += dp[i + 2];
}
return dp[0];
}
def num_decodings(s):
n = len(s)
dp = [0] * (n + 1)
dp[n] = 1
for i in range(n - 1, -1, -1):
if s[i] == '0': continue
dp[i] = dp[i + 1]
if i + 1 < n and (s[i] == '1' or (s[i] == '2' and s[i + 1] <= '6')): dp[i] += dp[i + 2]
return dp[0]
function numDecodings(s: string): number {
const n = s.length;
const dp = new Array<number>(n + 1).fill(0);
dp[n] = 1;
for (let i = n - 1; i >= 0; i--) {
if (s[i] === '0') continue;
dp[i] = dp[i + 1];
if (i + 1 < n && (s[i] === '1' || (s[i] === '2' && s[i + 1] <= '6'))) dp[i] += dp[i + 2];
}
return dp[0];
}
int numDecodings(string s) {
int next1 = 1, next2 = 0;
for (int i = (int)s.size() - 1; i >= 0; i--) {
int cur = 0;
if (s[i] != '0') {
cur = next1;
if (i + 1 < (int)s.size() && (s[i] == '1' || (s[i] == '2' && s[i + 1] <= '6'))) cur += next2;
}
next2 = next1;
next1 = cur;
}
return next1;
}
public int numDecodings(String s) {
int next1 = 1, next2 = 0;
for (int i = s.length() - 1; i >= 0; i--) {
int cur = 0;
if (s.charAt(i) != '0') {
cur = next1;
if (i + 1 < s.length() && (s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) <= '6'))) cur += next2;
}
next2 = next1;
next1 = cur;
}
return next1;
}
def num_decodings(s):
next1, next2 = 1, 0
for i in range(len(s) - 1, -1, -1):
cur = 0
if s[i] != '0':
cur = next1
if i + 1 < len(s) and (s[i] == '1' or (s[i] == '2' and s[i + 1] <= '6')): cur += next2
next2 = next1
next1 = cur
return next1
function numDecodings(s: string): number {
let next1 = 1, next2 = 0;
for (let i = s.length - 1; i >= 0; i--) {
let cur = 0;
if (s[i] !== '0') {
cur = next1;
if (i + 1 < s.length && (s[i] === '1' || (s[i] === '2' && s[i + 1] <= '6'))) cur += next2;
}
next2 = next1;
next1 = cur;
}
return next1;
}