Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #https://leetcode.com/problems/decode-ways/
- from functools import lru_cache
- class Solution:
- @lru_cache(maxsize=None)
- def numDecodingsHelper(self, s, index) -> int:
- if (index == len(s)):
- return 1
- if (s[index] == "0"):
- return 0
- if (index == len(s) - 1):
- return 1
- oneCount = self.numDecodingsHelper(s, index + 1)
- twoCount = 0
- twoDigit = int(s[index] + s[index+1])
- if (twoDigit >= 10 and twoDigit <= 26):
- twoCount = self.numDecodingsHelper(s, index + 2)
- return oneCount + twoCount
- def numDecodings(self, s: str) -> int:
- return self.numDecodingsHelper(s, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement