forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode_ways.py
47 lines (39 loc) · 997 Bytes
/
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
"""
A message containing letters from A-Z is being encoded to numbers using the
following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given a non-empty string containing only digits,
determine the total number of ways to decode it.
"""
def num_decodings(s: str):
"""
>> num_decodings("12")
2
>> num_decodings("226")
3
"""
if not s or int(s[0]) == 0:
return 0
last = 1
second_last = 1
for i in range(1, len(s)):
# 0 is a special digit since it does not
# correspond to any alphabet but can be
# meaningful if preceeded by 1 or 2
if s[i] == "0":
if s[i-1] in {"1", "2"}:
curr = second_last
else:
return 0
elif 11 <= int(s[i-1:i+1]) <= 26:
curr = second_last + last
else:
curr = last
last, second_last = curr, last
return last
if __name__ == "__main__":
import doctest
doctest.testmod()