Advertisement
bobbye

Decode Ways

Apr 17th, 2024
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. class Solution:
  2.     def numDecodings(self, s: str) -> int:
  3.         dp = {len(s) : 1}
  4.  
  5.         def dfs(i):
  6.             if i in dp:
  7.                 return dp[i]
  8.             if s[i] == "0":
  9.                 return 0
  10.            
  11.             numMappings = dfs(i + 1)
  12.  
  13.             if (i + 1 < len(s) and (s[i] == "1" or (s[i] == "2" and s[i+1] in "0123456"))):
  14.                 numMappings += dfs(i + 2)
  15.            
  16.             dp[i] = numMappings
  17.             return numMappings
  18.        
  19.         return dfs(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement