Dynamic Programming | Memoization | Java
\nclass Solution {\n    int mod=1000000007;\n    HashMap<Integer,Integer>map;\n    public int countTexts(String pressedKeys) {\n        HashSet<String>set=new H
```\nclass Solution {\n    int mod=1000000007;\n    HashMap<Integer,Integer>map;\n    public int countTexts(String pressedKeys) {\n        HashSet<String>set=new HashSet<>();\n        set.add("2");\n        set.add("22");\n        set.add("222");\n        set.add("3");\n        set.add("33");\n        set.add("333");\n        set.add("4");\n        set.add("44");\n        set.add("444");\n        \n        set.add("5");\n        set.add("55");\n        set.add("555");\n        set.add("6");\n        set.add("66");\n        set.add("666");\n        set.add("7");\n        set.add("77");\n        set.add("7777");\n        set.add("777");\n        set.add("8");\n        set.add("88");\n        set.add("888");\n        set.add("9");\n        set.add("99");\n        set.add("9999");\n        set.add("999");\n        \n        map=new HashMap<>();\n        return solver(pressedKeys,set,pressedKeys.length(),0);\n        \n    }\n    \n    public int solver(String str, HashSet<String>set, int n,int i){\n        \n        \n        if(i==n){\n            map.put(n,1);\n            return 1;\n        }\n        if(map.containsKey(i)){\n            return map.get(i);\n        }\n        int ans=0;\n        \n        for(int j=i+1;j<=n;j++){\n            String pre=str.substring(i,j);\n         //   String ros=j==n?"":str.substring(j);\n            if(set.contains(pre)){\n                if(map.containsKey(j)){\n                    ans+=map.get(j);\n                }else{\n                    ans+=solver(str,set,n,j);\n                }\n                \n                ans%=mod;\n            }else{\n                break;\n            }\n        }\n        \n        map.put(i,ans);\n        return ans;\n    }\n}```
['Dynamic Programming', 'Memoization', 'Java']