[C++] Dynamic programming solution
Key idea: total possible number of answer with a digit d at the position i = \n(total possible number of answer ends  at the position i-1 + interpret the last d
Key idea: total possible number of answer with a digit `d` at the position `i` = \n(total possible number of answer ends  at the position `i-1` + interpret the last digit as one character if 1 consecutive digits) + \n(total possible number of answer ends  at the position `i-2` + interpret the last two digits as one character if 2 consecutive digits) + \n(total possible number of answer ends  at the position `i-3` + interpret the last three digits as one character if 3 consecutive digits) + \n(total possible number of answer ends  at the position `i-4` + interpret the last four digits as one character if 4 consecutive digits) + \n\nOne character is composed of at most 4 digits, so we only have to look up at most 4 entries to fill in each entry of the memorization table.\n\n```\nint keys[10] = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4};\nconst int mod = 1e9 + 7;\n\nclass Solution {\npublic:\n    int countTexts(string pressedKeys) {\n        int n= pressedKeys.size();\n        if (n == 0) return 0;\n        int dp[n+1];\n        memset(dp, 0, sizeof(dp));\n        dp[0] = 1;\n        \n        for (int i = 1; i <= n; ++i) {\n            char c = pressedKeys[i-1];\n            for (int j = 0; j < keys[c-\'0\']; ++j) {\n                if (i-j <= 0 || pressedKeys[i-j-1] != c) {break;}\n                dp[i] = (dp[i] + dp[i-j-1]) % mod;\n            }\n        }\n        return dp[n];\n    }\n};\n```\nSpace complexity: O(n)\ntime complexity: O(n)
['Dynamic Programming', 'C']