Simple DP solution || JAVA || 34ms(Beats 100%)
We count consecutive repeating digits. For n repeating digits we count the number of ways to convert them into characters using DP.\nFor digits 7 and 9 there ar
We count consecutive repeating digits. For n repeating digits we count the number of ways to convert them into characters using DP.\nFor digits 7 and 9 there are 4 characters corresponding to them. Their ways of converting n digits is as follows:\nways[1]=1\nways[2]=2\nways[3]=4\nways[4]=8\n.\n.\n.\nways[n]=ways[n-1]+ways[n-2]+ways[n-3]+ways[n-4]\nFor digits other than 7 or 9, there are 3 characters corresponding to them. There ways of converting n repetitive digits is:\nways[1]=1\nways[2]=2\nways[3]=4\n.\n.\n.\nways[n]=ways[n-1]+ways[n-2]+ways[n-3]\nThe above formulas can be found out by taking some examples.\n\n```\nclass Solution {\n    public int countTexts(String pressedKeys) {\n        int count=1;\n        long total=1;\n        for(int i=1;i<pressedKeys.length();i++)\n        {\n            if(pressedKeys.charAt(i)!=pressedKeys.charAt(i-1))\n            {\n                if(pressedKeys.charAt(i-1)==\'7\'||pressedKeys.charAt(i-1)==\'9\')\n                    total=(total*count7or9(count))%1000000007;\n                else\n                    total=(total*countOthers(count)%1000000007);\n                count=1;\n            }\n            else\n                count++;\n        }\n        if(pressedKeys.charAt(pressedKeys.length()-1)==\'7\'||pressedKeys.charAt(pressedKeys.length()-1)==\'9\')\n            total=(total*count7or9(count))%1000000007;\n        else\n            total=(total*countOthers(count)%1000000007);\n        return (int)total;\n    }\n    public long count7or9(int count)\n    {\n        long[] ways=new long[count+1];\n        if(count==1)\n            return 1;\n        else if(count==2)\n            return 2;\n        else if(count==3)\n            return 4;\n        else if(count==4)\n            return 8;\n        ways[1]=1;\n        ways[2]=2;\n        ways[3]=4;\n        ways[4]=8;\n        for(int i=5;i<=count;i++)\n        {\n            ways[i]=(ways[i-1]+ways[i-2]+ways[i-3]+ways[i-4])%1000000007;\n        }\n        return ways[count];\n    }\n    public long countOthers(int count)\n    {\n        long[] ways=new long[count+1];\n        if(count==1)\n            return 1;\n        else if(count==2)\n            return 2;\n        else if(count==3)\n            return 4;\n        ways[1]=1;\n        ways[2]=2;\n        ways[3]=4;\n        for(int i=4;i<=count;i++)\n        {\n            ways[i]=(ways[i-1]+ways[i-2]+ways[i-3])%1000000007;\n        }\n        return ways[count];\n    }\n}\n```
['Dynamic Programming', 'Java']