You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#A message containing letters from A-Z is being encoded to numbers using the following mapping:
10
+
#
11
+
#'A' -> 1
12
+
#'B' -> 2
13
+
#...
14
+
#'Z' -> 26
15
+
#Given a non-empty string containing only digits, determine the total number of ways to decode it.
16
+
#
17
+
#Example 1:
18
+
#
19
+
#Input: "12"
20
+
#Output: 2
21
+
#Explanation: It could be decoded as "AB" (1 2) or "L" (12).
22
+
#
23
+
#Example 2:
24
+
#
25
+
#Input: "226"
26
+
#Output: 3
27
+
#Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
28
+
classSolution:
29
+
defnumDecodings(self, s):
30
+
iflen(s) ==0ors[0] =='0': #Condition-check: If str is empty or 1st digit is zero
31
+
return0#We return zero
32
+
tmpA, tmpB=1, 0#Initialize tmpA, tmpB counter
33
+
foriinrange(len(s)): #Loop through string
34
+
tmp=0#Initialize final counter tmp
35
+
ifs[i] !='0': #Condition-check: If str's 1st digit is not zero
36
+
tmp=tmpA#Update tmp by assigining tmpA
37
+
ifi>0and (s[i-1] =='1'or (s[i-1] =='2'ands[i] <='6')): #Condition-check: If i is greater than zero and previous digit is one or previous and current digit is between 2 and 6
38
+
tmp+=tmpB#Update tmp by adding tmpB into it
39
+
tmpA, tmpB=tmp, tmpA#Update tmpA, and tmpB by assigining tmp and tmpA
0 commit comments