Thinking process
Find the pattern
But be careful with 0
Mistakes
Instead of thinking of all possibilities, why not only thinking of the conditions that you need to do sth on it.
public class Solution {
public int numDecodings(String s) {
if (s.length() == 0 || s.charAt(0) == '0') return 0;
int[] dp = new int[s.length() + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= s.length(); ++i) {
int single = Integer.valueOf(s.substring(i - 1, i));
if (single >= 1 && single <= 9) {
dp[i] += dp[i - 1];
}
int twoNums = Integer.valueOf(s.substring(i - 2, i));
if (twoNums >= 10 && twoNums <= 26) {
dp[i] += dp[i - 2];
}
}
return dp[s.length()];
}
}