Advertisement
nirajs

decode way

Jan 2nd, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #https://leetcode.com/problems/decode-ways/
  2. from functools import lru_cache
  3.  
  4. class Solution:
  5. @lru_cache(maxsize=None)
  6. def numDecodingsHelper(self, s, index) -> int:
  7. if (index == len(s)):
  8. return 1
  9. if (s[index] == "0"):
  10. return 0
  11. if (index == len(s) - 1):
  12. return 1
  13. oneCount = self.numDecodingsHelper(s, index + 1)
  14. twoCount = 0
  15. twoDigit = int(s[index] + s[index+1])
  16. if (twoDigit >= 10 and twoDigit <= 26):
  17. twoCount = self.numDecodingsHelper(s, index + 2)
  18. return oneCount + twoCount
  19.  
  20. def numDecodings(self, s: str) -> int:
  21. return self.numDecodingsHelper(s, 0)
  22.  
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement