Skip to content

Commit

Permalink
decode-ways solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sun912 committed Aug 24, 2024
1 parent 227601a commit 4bff29b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions decode-ways/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
TC: O(n)
SC: O(n)
DNF, read solutions by Dale...ㅠㅠ
"""

class Solution:
def numDecodings(self, s: str) -> int:
memo = {len(s): 1}

def dfs(start):
if start in memo:
return memo[start]

if s[start] == "0":
return 0
if start + 1 < len(s) and int(s[start: start+2]) < 27:
memo[start] = dfs(start + 1) + dfs(start + 2)
else:
memo[start] = dfs(start + 1)
return memo[start]

return dfs(0)

0 comments on commit 4bff29b

Please sign in to comment.