The Woven Lights of Langmere
Personal Rating: Hard
# take in the number
code = input()
def get_count(code: str) -> int:
if not code or code[0] == '0':
return 0
decodings = [0] * (len(code) + 1) # Create array initialized with 0, with a length of len(code) + 1. decodings[0] is 1, which represents an empty string with 1 way of decoding. decodings[1] represents the number of possible decodings of number 1 etc.
decodings[0:2] = [1, 1] # Set the first two places to 1, since an empty code and the first letter can each only be decoded in one way
for i in range(2, len(code) + 1): # For each character in the code (after the first two):
if code[i-1] != '0': # If the previous character is not 0:
decodings[i] += decodings[i-1] # Add the value previous to decodings[i] to decodings[i]. In the first case i=2, decodings[1] which is 1, is added to decodings[2], which is 0 at the start.
if 10 <= int(code[i-2:i]) <= 26: # If the value of the previous two characters is between 10 and 26 (inclusive):
decodings[i] += decodings[i-2] # Add the second last value before decodings[i] to decodings[i]. In the first non-trivial case i=3, decodings[1], which is 1, is added to decodings[3], which is 0.
return decodings[len(code)]%1000000007 # Return the value in decodings at the index "length of code" %1000000007
print(get_count(code))
Last updated