Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Alice is texting Bob using her phone. The mapping of digits to letters is shown in the figure below.
In order to add a letter, Alice has to press the key of the corresponding digit i
times, where i
is the position of the letter in the key.
's'
, Alice has to press '7'
four times. Similarly, to add the letter 'k'
, Alice has to press '5'
twice.'0'
and '1'
do not map to any letters, so Alice does not use them.However, due to an error in transmission, Bob did not receive Alice’s text message but received a string of pressed keys instead.
"bob"
, Bob received the string "2266622"
.Given a string pressedKeys
representing the string received by Bob, return the total number of possible text messages Alice could have sent.
Since the answer may be very large, return it modulo 109 + 7
.
Example 1:
Input: pressedKeys = "22233"
Output: 8
Explanation:
The possible text messages Alice could have sent are:
"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae", and "ce".
Since there are 8 possible messages, we return 8.
Example 2:
Input: pressedKeys = "222222222222222222222222222222222222"
Output: 82876089
Explanation:
There are 2082876103 possible text messages Alice could have sent.
Since we need to return the answer modulo 109 + 7, we return 2082876103 % (109 + 7) = 82876089.
Constraints:
1 <= pressedKeys.length <= 105
pressedKeys
only consists of digits from '2'
– '9'
.class Solution(object):
def countTexts(self, pressedKeys):
"""
:type pressedKeys: str
:rtype: int
"""
dp = [1] + [0]*len(pressedKeys)
mod = 10**9 + 7
for i, n in enumerate(pressedKeys):
dp[i+1] = dp[i]
# check if is continous
if i >= 1 and pressedKeys[i-1] == n:
dp[i+1] += dp[i-1]
dp[i+1] %= mod
if i >= 2 and pressedKeys[i-2] == n:
dp[i+1] += dp[i-2]
dp[i+1] %= mod
# Special case for '7' and '9' that can have 4 characters combination
if i >= 3 and pressedKeys[i-3] == n and (n == "7" or n == "9"):
dp[i+1] += dp[i-3]
dp[i+1] %= mod
return dp[-1]
const int mod = 1e9 + 7;
const int mxx = 1e5 + 1;
#define ll long long
class Solution {
public:
vector<int> a, b;
Solution()
{
a.resize(mxx, 0);
a[1] = 1;
a[2] = 2;
a[3] = 4;
for(int i=4; i<=1e5; i++)
{
a[i] = ((a[i-1] + a[i-2]) % mod + a[i-3]) % mod;
}
b.resize(mxx, 0);
b[1] = 1;
b[2] = 2;
b[3] = 4;
b[4] = 8;
for(int i=5; i<=1e5; i++)
{
b[i] = ((((b[i-1] + b[i-2]) % mod + b[i-3]) % mod) + b[i-4]) % mod;
}
}
int countTexts(string s) {
int n = s.size();
int ans = 1;
int cnt = 1;
int i = 0;
while(i+1 < n)
{
while(i+1 < n && s[i] == s[i+1])
{
cnt++;
i++;
}
// cout << a[cnt] << " ";
if(s[i] != '7' && s[i] != '9')
ans = ((ll)ans * (ll)a[cnt]) % mod;
else
ans = ((ll)ans * (ll)b[cnt]) % mod;
cnt = 1;
i++;
}
return ans;
}
};
class Solution {
Long[][] mem = null;
public int countTexts(String p) {
mem = new Long[p.length()+1][10];
Stack<int[]> arr = new Stack();
for(int i=0; i<p.length(); i++) {
if (arr.size() == 0 || arr.peek()[0] != (p.charAt(i)-'0')) {
arr.push(new int[]{p.charAt(i)-'0', 1});
} else {
arr.peek()[1]++;
}
}
ArrayList<int[]> list = new ArrayList(arr);
long prod = 1;
for(int[] x: list) {
prod *= dp(x[1], x[0])%1000000007;
prod = prod % 1000000007;
}
return (int)(prod%1000000007);
}
long dp(int c, int l) {
int[] let = new int[]{0,0,3,3,3,3,3,4,3,4};
if (c <= 0) return 1;
if (mem[c][l] != null) return mem[c][l];
long ans = 0;
for(int i=1; i<=let[l]; i++) {
if (c >=i) ans += dp(c-i, l)%1000000007;
ans = ans%1000000007;
}
mem[c][l] = ans;
return ans;
}
}
In our experience, we suggest you solve this Count Number of Texts and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Count Number of Texts LeetCode Solution or any coding solution.
I hope this Count Number of Texts LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>