-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsolution.py
More file actions
29 lines (22 loc) · 907 Bytes
/
Copy pathsolution.py
File metadata and controls
29 lines (22 loc) · 907 Bytes
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
class Solution:
# Time: O(n)
# Space: O(1)
def num_decodings(self, s: str) -> int:
if not s:
return 0
num_ways_two_steps_behind: int = 1
num_ways_one_step_behind: int = 0 if s[0] == "0" else 1
for index in range(1, len(s)):
current_char: str = s[index]
previous_char: str = s[index - 1]
current_num_ways: int = 0
if current_char != "0":
current_num_ways += num_ways_one_step_behind
two_digit_value: int = int(previous_char + current_char)
if previous_char != "0" and 10 <= two_digit_value <= 26:
current_num_ways += num_ways_two_steps_behind
num_ways_two_steps_behind, num_ways_one_step_behind = (
num_ways_one_step_behind,
current_num_ways,
)
return num_ways_one_step_behind