DP solution using O(1) memory
Code\ncpp []\nclass Solution {\npublic:\n    static constexpr int combinations[10] = { 0, 0, 3, 3, 3, 3, 3, 4, 3, 4 };\n    static constexpr int mod = 100000000
# Code\n```cpp []\nclass Solution {\npublic:\n    static constexpr int combinations[10] = { 0, 0, 3, 3, 3, 3, 3, 4, 3, 4 };\n    static constexpr int mod = 1000000007;\n    int countTexts(string& pressedKeys) {\n        int dp[6] = {1, 1};\n\n        int len = 2, maxLen = combinations[pressedKeys[0]-\'0\'];\n        for (int i = 1; i < pressedKeys.size(); i++) {\n            if (pressedKeys[i] != pressedKeys[i-1]) {\n                maxLen = combinations[pressedKeys[i]-\'0\'];\n                dp[0] = dp[1] = dp[len-1];\n                dp[2] = dp[3] = dp[4] = 0;\n                len = 2;\n                continue;\n            }\n\n            // Calculate combinations\n            for (int j = 1; j <= len; j++) {\n                dp[len] += dp[len-j];\n                dp[len] %= mod;\n            }\n\n            // Shift calculated combinations to the left, also reset dp[len] to zero\n            if (len == maxLen)\n                for (int i = 0; i <= len; i++)\n                    dp[i] = dp[i+1];\n\n            // Increase len if it less than maxLen\n            len = min(len + 1, maxLen);\n        }\n        return dp[len-1];\n    }\n};\n```
['C++']