#91
Decode Ways
medium· 1-D DPruns: 0A message containing letters from A-Z can be encoded into numbers using the mapping 'A' to 1, 'B' to 2, ..., 'Z' to 26. To decode an encoded message, the digits must be grouped and mapped back into letters. Given a string s of digits, return the number of ways to decode it. The test cases are generated so the answer fits in a 32-bit integer.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 405
class Solution:
def numDecodings(self, s: str) -> int:
if not s or s[0] == "0":
return 0
prev, curr = 1, 1
for i in range(1, len(s)):
new = 0
if s[i] != "0":
new += curr
two = int(s[i - 1 : i + 1])
if 10 <= two <= 26:
new += prev
prev, curr = curr, new
return curr
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.