Hello guys,
I am solving LeedCode Problem 91 - Decode ways, and I have been able to write a backtracking(recursive) solution for this solution, hoping that I would get the formula that I would use it to optimize into a DP solution. However, I am not able to understand how to convert to a DP solution for this problem. Here's is my recursive code, help is much appreciated :)
public int decode(String s)
{
int result = 0;
if(s.length()==0)
return 1;
for(int i=1;i<=s.length();i++)
{
String left = s.substring(0,i);
String right = s.substring(i);
if(mapping.contains(left))
{
result= result+decode(right);
}
}
return result;
}
public void initializeMapping()
{for(int i =1;i<=26;i++){mapping.add(i+"");}
}Thanks.