Solution T=O(n) S=O(1) explain with schematic
\ndp[] is the possibilities of text combination.\nfrom the example, you can understand that \ndp[i+1]+=dp[i]; // it always can be a letter alone and do not forg
![image](https://assets.leetcode.com/users/images/f6c8d425-39ab-404f-8de8-3090d74572ea_1652003566.5879638.png)\ndp[] is the possibilities of text combination.\nfrom the example, you can understand that \ndp[i+1]+=dp[i]; // it always can be a letter alone and do not forget the dp[-1] should set to 1\n- dp[i-1],dp[i-2] //if it can combine with the former pressed\n- dp[i-3] //if this number is 7 or 9 \n\nthe Mod operation should be executed whenever it is added, cause the number will be very large at late stage. \nP.S\uFF1AIf the number is less than MOD, it will not be affected.\n\n```\n\tprivate static final int MOD = 1_000_000_007;\n    public int countTexts(String pressedKeys) {\n        //the last we need is i-4, so 5 is enough.\n        int[] textPossibility = new int[5]; \n        char[] chars = pressedKeys.toCharArray();\n        int n = pressedKeys.length();\n        textPossibility[0]=1;\n        for(int i=0;i<n;i++){\n            char c = pressedKeys.charAt(i);\n            int index = (i+1)%5;\n            //suppose the letter is the 1 time pressed\n            textPossibility[index] = textPossibility[i%5]; \n            if(i>0 && c==pressedKeys.charAt(i-1)){\n                textPossibility[index]+= textPossibility[(i-1)%5];\n                textPossibility[index] %= MOD;\n                if(i>1 && c==pressedKeys.charAt(i-2)){\n                    textPossibility[index]+= textPossibility[(i-2)%5];\n                    textPossibility[index] %= MOD;\n                    if((c==\'7\'||c==\'9\') && i>2 && c==pressedKeys.charAt(i-3)){\n                        textPossibility[index]+= textPossibility[(i-3)%5];\n                        textPossibility[index] %= MOD;\n                    }\n                }\n            }\n        }\n        return textPossibility[n%5];\n    }\n```
['Dynamic Programming', 'Java']