1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { using LL = long long; const int MOD = 1e9 + 7; public: int countTexts(string pressedKeys) { int n = pressedKeys.size(); LL a = 1, b = 0, c = 0, d = 0; for (int i = 1; i < n; ++i) { char x = pressedKeys[i]; if (x != pressedKeys[i - 1]) { a = (a + b + c + d) % MOD; b = 0, c = 0, d = 0; } else { LL t = (a + b + c + d) % MOD; if (x == '7' || x == '9') d = c; c = b; b = a; a = t; } } return (a + b + c + d) % MOD; } };
|