s
and the current index index
as arguments, along with a memoization dictionary memo
.index
is equal to the length of s
, return 1 as we have found a valid way to decode the entire string.index
is '0', return 0 as it cannot lead to a valid decoding.index
is already in memo
, return the stored result to avoid redundant calculations.ans
to store the number of decodings.index + 1
to consider single digit decoding and add the result to ans
.index + 2
to consider double digit decoding and add the result to ans
.memo
with the key as the current index
.ans
.