-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode-variations.py
executable file
·102 lines (85 loc) · 2.58 KB
/
decode-variations.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
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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 9 17:05:21 2020
@author: johnoyegbite
"""
# SOLVED!
"""
Problem:
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.
Example 1:
Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
Example 2:
Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6),
or "BBF" (2 2 6).
Solution Visualization:
Input: "1226"
Output: 5 [=> (ABBF), (ABZ), (AVF), (LBF), (LZ)]
1226 => 1,2,2,6 (ABBF)| 1,2,26 (ABZ)| 1,22,6 (AVF)| 12,2,6 (LBF)| 12,26 (LZ)
.
1 2 2 6
---
/ \
. .
1 2 2 6 1 2 2 6
--- ---
/ \ / \
. . .
1 2 2 6 1 2 2 6 1 2 2 6 P
--- --- -
/ \ / \ / \
.
1 2 2 6 P P F P F
-
/ \
P F
Where P represent a vaild path
F represent a failed path
"""
class Solution:
def numDecodings(self, s: str) -> int:
# To memoize, comment the path, result aspect.
def decode_help(s, i, path, result, memo={}):
# if i in memo:
# return memo[i]
if i == len(s):
result.append(",".join(path[:]))
return 1
if i > len(s):
return 0
left, right = 0, 0
if 0 < int(s[i]) < 10:
path.append(s[i])
left = decode_help(s, i+1, path, result, memo)
path.pop()
if 10 <= int(s[i:i+2]) <= 26:
path.append(s[i:i+2])
right = decode_help(s, i+2, path, result, memo)
path.pop()
total = left + right
# memo[i] = total
return total
path = []
result = []
total = decode_help(s, 0, path, result)
print(result)
print()
return total
if __name__ == "__main__":
s = Solution()
S = "2263"
# S = "1226"
# S = "1206"
S = "122121"
print(s.numDecodings(S))