#Gotcha: because int are immutable in python, changing it in a function does not update its value outside of this method
# therefore, we have to return its value from the function that changes it

class Solution(object):
    def decode_ways(self, nums):
        """
        :type nums: str
        :rtype: int
        """

        if not nums:
            return 0

        result = 0

        result = self.decode_ways_count(nums, 0, result)

        return result

    def decode_ways_count(self, nums, index, result):
        # went through all the digits in the string, increment the result count
        if index == len(nums):
            result += 1
        elif nums[index] != '0':
            # The digit at index is not 0, I can use it to get a character
            result = self.decode_ways_count(nums, index + 1, result)

            # Check if we can build a character using 2 digits
            if index < len(nums) - 1 and 10 <= int(nums[index: index + 2]) <= 26:
                result = self.decode_ways_count(nums, index + 2, result)
        
        return result
    
    def get_decode_ways(self, code):
        """
        type code: str
        rtype: int
        """
        if not code:
            return 0

        ways = [0 for _ in range(len(code) + 1)]
        ways[0] = 1
        if code[0] != '0':
            ways[1] = 1
        
        for i in range(1, len(code)):
            if code[i] != '0':
                ways[i + 1] += ways[i]
            
            if 10 <= int(code[i-1: i+1]) <= 26:
                ways[i + 1] += ways[i - 1]
        
        return ways[-1]

solution = Solution()
print(solution.decode_ways('12123'))
print(solution.get_decode_ways('12123'))
