public class Solution { /** * @param s: a string, encoded message * @return: an integer, the number of ways decoding */ public static int numDecodings(String s) { // write your code here if(s.length()==0) return 0; int[] dp = new int[s.length()+1]; dp[0]=1; for(int i=1;i<=s.length();++i){ int num2 = s.charAt(i-1)-'0'; if(num2>0&&num2<=9){ dp[i]+=dp[i-1]; } if(i-2>=0) { int num1 = s.charAt(i - 2) - '0'; int num3 = num1 * 10 + num2; if (num3 >= 10 && num3 <= 26) { dp[i] += dp[i - 2]; } } } return dp[s.length()]; } public static void main(String[] args) { int res = numDecodings("12"); System.out.println(res); } }