forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmjuu.py
31 lines (23 loc) ยท 814 Bytes
/
pmjuu.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
class Solution:
def numDecodings(self, s: str) -> int:
if s[0] == "0":
return 0
prev, curr = 1, 1
for i in range(1, len(s)):
temp = curr
if s[i] == "0":
if s[i - 1] in ("1", "2"):
curr = prev
else:
return 0
else:
two_num = int(s[i - 1] + s[i])
is_two_num_decoded = 10 <= two_num <= 26
if is_two_num_decoded:
curr += prev
prev = temp
return curr
# Time Complexity: O(n)
# - The loop iterates through the string once, where n is the length of the string.
# Space Complexity: O(1)
# - Only two variables (prev and curr) are used, independent of the input size.