Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded.
For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'.
You can assume that the messages are decodable. For example, '001' is not allowed.
class DailyCodingProblem7 {
public static void main(String args[]) {
String message = "11111111";
int n=message.length();
Integer[] ways=new Integer[n+1];
int res = solution(message,n,ways);
System.out.println(res);
}
private static int solution(String message,int k,Integer[] ways) {
int n=message.length();
if(k==0)
{
return 1;
}
if((int)message.charAt(n-k)==0)
{
return 0;
}
if(ways[k]!=null)
{
System.out.println(k+" : " +ways[k]);
return ways[k];
}
ways[k]=solution(message,k-1,ways);
if(k>=2 && Integer.valueOf(message.substring(n-k,n-k+2))<26)
ways[k]+=solution(message,k-2,ways);
return ways[k];
}
}
If you observe the flow, we can notice ways[k]
only needs the most recent 2 values this ways[k-1]
and ways[k-2]
. Should I use a Hashmap or there is a better approach?
1 : 1
2 : 2
3 : 3
4 : 5
5 : 8
6 : 13
34