13. Dcode Message
def decodeWays(msg):
return r_decodeWays(msg, 0)
def r_decodeWays(msg, count):
# 考慮msg長度=3的情況
if len(msg) == 3:
count = 0
addCount = True
# 1. 每個char都不能為0
for c in msg:
if int(c) == 0:
addCount = False
if addCount: count += 1
# 2. 將長度切成2,1
a = int(msg[0:2])
b = int(msg[2:])
if a >= 10 and a <= 26 and b > 0:
count += 1
# 3. 將長度切成1,2
a = int(msg[0:1])
b = int(msg[1:])
if a > 0 and b >= 10 and b <= 26:
count += 1
return count
else:
return r_decodeWays(msg[:3], count) + r_decodeWays(msg[3:], count)Last updated