Any help is really appreciated


#1

public class Solution {
public int numDecodings(String A) {
if(A==null)
return 0;
int n=A.length();
if(n==0)
return 0;
if(A.charAt(0)==‘0’)
return 0;
long[] dp=new long[n+1];
dp[0]=1;
long mod=1000000007;
dp[1]=1;
for(int i=2;i<=n;i++)
{
if(A.charAt(i-1)>‘0’)
dp[i]=dp[i-1];
if(A.charAt(i-2)==‘1’||(A.charAt(i-2)==‘2’&&A.charAt(i-1)<‘7’))
dp[i]=(dp[i]+dp[i-2])%mod;
}
return (int)(dp[n]%mod);
}
}
This works fine but if I apply same technique while doing from right to left, it wont work ? why?