forked from MTrajK/coding-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_of_smses.py
More file actions
84 lines (60 loc) · 1.62 KB
/
number_of_smses.py
File metadata and controls
84 lines (60 loc) · 1.62 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'''
Number of SMSes
Given the number sequence that is being typed in order to write and SMS message, return the count
of all the possible messages that can be constructed.
1 2 3
abc def
4 5 6
ghi jkl mno
7 8 9
pqrs tuv wxyz
The blank space character is constructed with a '0'.
Input: '222'
Output: 4
Output explanation: '222' could mean: 'c', 'ab','ba' or 'aaa'. That makes 4 possible messages.
=========================================
Dynamic programming solution. Similar to number_of_decodings.py.
Time Complexity: O(N)
Space Complexity: O(N)
'''
############
# Solution #
############
def num_smses(sequence):
n = len(sequence)
dp = [0] * n
# dp starting values, check all 4 possible starting combinations
for i in range(min(4, n)):
if is_valid(sequence[0 : i+1]):
dp[i] = 1
# run dp
for i in range(1, n):
# check all 4 possible combinations (x, xx, xxx, xxxx)
for j in range(min(4, i)):
if is_valid(sequence[i-j : i+1]):
dp[i] += dp[i - j - 1]
return dp[n - 1]
def is_valid(sequence):
ch = sequence[0]
for c in sequence:
if c != ch:
return False
if sequence == '0':
return True
if ((ch >= '2' and ch <= '6') or ch == '8') and (len(sequence) < 4):
return True
if (ch == '7') or (ch == '9'):
return True
return False
###########
# Testing #
###########
# Test 1
# Correct result => 4
print(num_smses('222'))
# Test 2
# Correct result => 14
print(num_smses('2202222'))
# Test 3
# Correct result => 274
print(num_smses('2222222222'))