C++ O(n) Solution with Explanation
At any point you have couple of things to do and that is you can choose current character \n\n1. As a single character so the answer will be what was at i-1 ind
At any point you have couple of things to do and that is you can choose current character \n\n1. As a single character so the answer will be what was at i-1 index because you\'re adding current character as single character \n\n2. Taking current character with last so i-2 will be the added to the answer\n3. Taking till i-2 character so i-3 will be added to answer\n4. In case of 7, 9 Taking i-3 character so i-4 will be added to the answer\n\nHere is an example to that\n\nsuppose we have 222\n\nfor i = 0 we can only get \na\n\nfor i = 1 we can get\naa b\n\nfor i = 2 we can get\n\n(First taking current character as single)\naa-a (2-2-2)\nb-a (22-2)\n\n(Taking current with last)\na-b (2-22)\n\nTaking till i-2\nc (222)\n\nIn the solution we\'ll first calculate the combinations\n\n\n```\n#define mod 1000000007\n#define ll long long int\nclass Solution {\npublic:\n    int countTexts(string s) {\n        \n        int n = s.size();\n        \n        if(n<3){\n            return n;\n        }\n        \n        ll ans = 1;\n        vector<ll> dp1(n+1,0);\n        vector<ll> dp2(n+1,0);\n        \n        dp1[0] = 1;\n        dp1[1] = 1;\n        dp1[2] = 2;\n        \n        dp2[0] = 1;\n        dp2[1] = 1;\n        dp2[2] = 2;\n        dp2[3] = 4;\n        \n        for(int i = 3 ; i <= n ; i++)    \n            dp1[i] = (dp1[i-1] + dp1[i-2] + dp1[i-3])%mod;\n        \n        for(int i = 4 ; i <= n ; i++)    //for 7 & 9\n            dp2[i] = (dp2[i-1] + dp2[i-2] + dp2[i-3] + dp2[i-4])%mod;\n        \n        int i = 0;\n        \n        int start = -1;\n        \n        while(i<n-1){\n            \n            while(i<n-1 && s[i]==s[i+1])//finding the length same consecutive characters\n                i++;\n            \n            int len = i-start;\n            \n            if(s[i]==\'7\' || s[i]==\'9\')\n                ans = (ans*dp2[len])%mod; //multiplying because for 2 different consecutive characters we can\'t add for ex if at first place we have 2 options and for place two also 2 option then if we combine we\'ll have 4 options in total for better understanding see example 1 of the question\n            else\n                ans = (ans*dp1[len])%mod;\n            \n            start = i;\n            \n            i++;\n        }\n        \n        return ans;\n    }\n};\n```
[]