C++ top-down recursive and memoization solution
\nclass Solution \n{\n    public:\n    int dp[100001][5];\n    long long d=1000000007;\n    int get(int n,int k)\n    {\n        if(n<0) return 0; // not valid\
```\nclass Solution \n{\n    public:\n    int dp[100001][5];\n    long long d=1000000007;\n    int get(int n,int k)\n    {\n        if(n<0) return 0; // not valid\n        if(n==0) return 1; // valid\n        if(dp[n][k]!=-1) return dp[n][k];\n\n        long long result=0;\n        for(int i=1;i<=k;i++) result=(result+get(n-i,k))%d; // i pressed counts are used\n        dp[n][k]=result;\n        return result;\n    }\n    int countTexts(string pressedKeys) \n    {\n        memset(dp,-1,sizeof(dp));\n        long long result=1,a[10],n=pressedKeys.length(),c=1;\n\n        for(int i=0;i<10;i++) a[i]=3; // a[i] is the number of available alphabets for a given digit i\n        a[7]=4;\n        a[9]=4;\n        pressedKeys+="0"; // add a never-appeared digit at the end to make the below code concise\n        \n        for(int i=1;i<n+1;i++)\n        {\n            if(pressedKeys[i]==pressedKeys[i-1]) c++; // same digits\n            else result=(result*get(c,a[pressedKeys[i-1]-\'0\']))%d, c=1; // find a different digit, so process the previous same digits\n        }\n\n        return result;\n    }\n};\n```
[]