Easy JAVA DP Solution | Similar to Decode Ways | Memoization
Consider taking a hashmap to store the characters corresponding to number of keypresses. Say if we press 3 twice, we store e in the hashmap as 33 => \'e\'.  Now
Consider taking a hashmap to store the characters corresponding to number of keypresses. Say if we press 3 twice, we store e in the hashmap as 33 => \'e\'.  Now we have put all the possible characters in the hashmap. \n\nWe now solve the problem like we did in [Decode Ways I](https://leetcode.com/problems/decode-ways/) and maintain the count, before storing the count in the DP make sure to take mod of the answer by 10^9 + 7.\n\n```\nclass Solution {\n    \n        int mod = (int)1e9 + 7;\n    public int countTexts(String keys, int idx, HashMap<Integer, Character> map, int[] dp) {\n        \n        \n        if(idx >= keys.length()) {\n            return 1;\n        }   \n        \n        if(dp[idx] != -1) {\n            return dp[idx];\n        }\n        \n        int count = 0;\n        char ch = keys.charAt(idx);\n        \n        if(ch != \'0\' || ch != \'1\') {\n            count = (count + countTexts(keys, idx + 1, map, dp)) % mod;\n        }\n        \n        if(idx < keys.length() - 1) {\n            int num = (ch - \'0\') * 10 + (keys.charAt(idx + 1) - \'0\');\n            if(map.containsKey(num)) {\n                count = (count + countTexts(keys, idx + 2, map, dp)) % mod;\n            }\n        }\n        \n        if(idx < keys.length() - 2) {\n            int num = (ch - \'0\') * 100 + (keys.charAt(idx + 1) - \'0\') * 10 + (keys.charAt(idx + 2) - \'0\');\n            if(map.containsKey(num)) {\n                count = (count + countTexts(keys, idx + 3, map, dp)) % mod;\n            }\n        }\n        \n        if(idx < keys.length() - 3) {\n            int num = (ch - \'0\') * 1000 + (keys.charAt(idx + 1) - \'0\') * 100 + (keys.charAt(idx + 2) - \'0\') * 10 + (keys.charAt(idx + 3) - \'0\');\n            if(map.containsKey(num)) {\n                count = (count + countTexts(keys, idx + 4, map, dp)) % mod;\n            }\n        }\n        \n        return dp[idx] = count % mod;\n    }\n    \n    public int countTexts(String pressedKeys) {\n        \n        HashMap<Integer, Character> map = new HashMap<>();\n        map.put(2, \'a\');\n        map.put(22, \'b\');\n        map.put(222, \'c\');\n        map.put(3, \'d\');\n        map.put(33, \'e\');\n        map.put(333, \'f\');\n        map.put(4, \'g\');\n        map.put(44, \'h\');\n        map.put(444, \'i\');\n        map.put(5, \'j\');\n        map.put(55, \'k\');\n        map.put(555, \'l\');\n        map.put(6, \'m\');\n        map.put(66, \'n\');\n        map.put(666, \'o\');\n        map.put(7, \'p\');\n        map.put(77, \'q\');\n        map.put(777, \'r\');\n        map.put(7777, \'s\');\n        map.put(8, \'t\');\n        map.put(88, \'u\');\n        map.put(888, \'v\');\n        map.put(9, \'w\');\n        map.put(99, \'x\');\n        map.put(999, \'y\');\n        map.put(9999, \'z\');\n        \n        int[] dp = new int[pressedKeys.length() + 1];\n        Arrays.fill(dp, -1);\n        return countTexts(pressedKeys, 0, map, dp);\n    }\n}\n```
['Dynamic Programming', 'Depth-First Search', 'Memoization', 'Java']