Skip to content

Commit

Permalink
feat solve decode-ways
Browse files Browse the repository at this point in the history
  • Loading branch information
pmjuu committed Dec 19, 2024
1 parent 680ac3a commit 857d78f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions decode-ways/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
def numDecodings(self, s: str) -> int:
if s[0] == "0":
return 0

prev, curr = 1, 1

for i in range(1, len(s)):
temp = curr

if s[i] == "0":
if s[i - 1] in ("1", "2"):
curr = prev
else:
return 0
else:
two_num = int(s[i - 1] + s[i])
is_two_num_decoded = 10 <= two_num <= 26
if is_two_num_decoded:
curr += prev

prev = temp

return curr


# Time Complexity: O(n)
# - The loop iterates through the string once, where n is the length of the string.

# Space Complexity: O(1)
# - Only two variables (prev and curr) are used, independent of the input size.

0 comments on commit 857d78f

Please sign in to comment.