-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0639_decode_ways_2.py
More file actions
188 lines (151 loc) · 3.91 KB
/
0639_decode_ways_2.py
File metadata and controls
188 lines (151 loc) · 3.91 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# ------------------------------------------------------------------------------
# Question:0639_decode_ways_2.py
# ------------------------------------------------------------------------------
# tags:
'''
A message containing letters from A-Z is being encoded to numbers using the
following mapping way:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Beyond that, now the encoded string can also contain the character '*', which
can be treated as one of the numbers from 1 to 9.
Given the encoded message containing digits and the character '*', return the
total number of ways to decode it.
Also, since the answer may be very large, you should return the
output mod 109 + 7.
Example 1:
Input: "*"
Output: 9
Explanation: The encoded message can be decoded to the string:
"A", "B", "C", "D", "E", "F", "G", "H", "I".
Example 2:
Input: "1*"
Output: 9 + 9 = 18
Note:
The length of the input string will fit in range [1, 105].
The input string will only contain the character '*' and digits '0' - '9'.
'''
# ------------------------------------------------------------------------------
# Solutions
# ------------------------------------------------------------------------------
import unittest
from typing import *
class Solution:
'''
3
/
*3
/
2*3
/
12*3
\
3 => 1
/
*3 =>
\
'' =>
for len of 2 having *:
*1...6 => *->[1,2] = 2 * 6
*7...9 => *->[1] = 9
ex: 1* , 9* , 11 , 19
if index is a number:
branch1 = recur and increment index by 1
if first > 2 => don't branch
branch2 = recur and increment index by 2
if first = 1 and second = * => 9 * branch2
if first = 2 and second = * => 6 * branch2
if second
return branch1 + branch2
if index is a *:
branch1 = recur and increment index by 1
branch2 = recur and increment index by 2
return branch1 + branch2
'''
def numDecodings(self, s: str) -> int:
pass
class Solution:
'''
Time:
Space:
3=>1
/
*3=>1 + 1 * 9
\
/ ''=>1
2*3=>11
\ *=>9
/
**=>11
\
/ ''=> 1
12*3=>
\ 3=>1
/
*3=>
\
''=>1
*=> 9
/
1*=>18
\
''=> 1
* = 9
1* = 18
2* = 6
*?
? 1->6 => 2
? 7->9 => 1
1
*1=>9 * 1 + 2
1
*1
**1
1
'''
def numDecodings(self, s: str) -> int:
def dfs(cur):
if not cur:
return 1
if cur[0] == '0':
return 0
if len(cur) == 1:
if cur == '*':
return 9
return 1
if len(cur) == 2:
if cur == '1*':
return 18
elif cur == '2*':
return 6
elif cur[0] == '*':
return 2 if int(cur[1]) <= 6 else 1
multi = 1
left = dfs(cur[1:])
if cur[0] != '*':
# 1*1, 1*9, 111, 999
if cur[1] == '*' or int(cur[:2]) <= 26:
right = dfs(cur[2:])
else:
# *11, *99, **1
if int(cur[1]) <= 6 or cur[:2] =='**':
right = dfs(cur[2:])
if cur[0] == '*':
multi = 9
return (left + right) * multi
d = {
'*': 9,
'1*': 18,
'2*': 6,
}
return dfs(s)
# ------------------------------------------------------------------------------
# Tests
# ------------------------------------------------------------------------------
class TestSolution(unittest.TestCase):
def test_simple(self):
s = Solution()
self.assertEqual(True, True)
unittest.main(verbosity=2)