-
Notifications
You must be signed in to change notification settings - Fork 263
/
091 Decode Ways.py
60 lines (48 loc) · 1.49 KB
/
091 Decode Ways.py
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
"""
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
The number of ways decoding "12" is 2.
"""
__author__ = 'Danyang'
class Solution(object):
def numDecodings(self, s):
"""
F
Let F[i] be the number of decode ways for s[:i]
- 1 2 2 3 1 2 2 3
1 1 2 ? ?
F[i] = (F[i-1]) + optional(F[i-2]))
notice the special handling for "0
:param s: a string
:return: an integer
"""
if s.startswith("0"):
return 0
n = len(s)
if not s:
return 0
F = [0 for _ in xrange(n+1)]
F[0] = 1
F[1] = 1
for i in xrange(2, n+1):
if s[i-1] != "0":
F[i] = F[i-1]
if 10 <= int(s[i-2]+s[i-1]) < 27:
F[i] += F[i-2]
else: # 0 is special
if s[i-2] in ("1", "2"):
F[i] = F[i-2]
else:
return 0
return F[-1]
if __name__ == "__main__":
assert Solution().numDecodings("10") == 1
assert Solution().numDecodings("27") == 1
assert Solution().numDecodings("12") == 2
assert Solution().numDecodings("0") == 0