Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded 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&gt;=2 &amp;&amp; Integer.valueOf(message.substring(n-k,n-k+2))&lt;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
Houston, you have some bugs

The algorithm counts incorrectly in some cases involving zeros.
For example, there's only one way to make "10" or "20", not 2.

A different problem is the overly strict condition &lt;26,
which excludes "26" even it can be decoded to z.

Beware of string slicing

String.substring creates a new string.
This could become expensive when repeated often.
If you change the logic to work with a char[] instead of a String,
then you can check if the first digit is '1',
or the first digit is '2' and the second digit is &lt;= '6'.

Unnecessary code

This condition will never be true:


if((int)message.charAt(n-k)==0)



The characters in the input are in the range of '0'..'9',
therefore their int values are in the range of 48..57.

Unnecessary Integer[]

You could safely replace Integer[] with int[],
and use a condition on zero value instead of null value.