Decode Ways

Problem

https://leetcode.com/problems/decode-ways/

You have intercepted a secret message encoded as a string of numbers. The message is decoded via the following mapping:

"1" -> 'A'
"2" -> 'B'
...
"25" -> 'Y'
"26" -> 'Z'

However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes ("2" and "5" vs "25").

For example, "11106" can be decoded into:

  • "AAJF" with the grouping (1, 1, 10, 6)

  • "KJF" with the grouping (11, 10, 6)

  • The grouping (1, 11, 06) is invalid because "06" is not a valid code (only "6" is valid).

Note: there may be strings that are impossible to decode.
Given a string s containing only digits, return the number of ways to decode it. If the entire string cannot be decoded in any valid way, return 0.
The test cases are generated so that the answer fits in a

32-bit

integer.

Example 1:

Input: s = “12”

Output: 2

Explanation:

“12” could be decoded as “AB” (1 2) or “L” (12).

Example 2:

Input: s = “226”

Output: 3

Explanation:

“226” could be decoded as “BZ” (2 26), “VF” (22 6), or “BBF” (2 2 6).

Example 3:

Input: s = “06”

Output: 0

Explanation:

“06” cannot be mapped to “F” because of the leading zero (“6” is different from “06”). In this case, the string is not a valid encoding, so return 0.

Constraints:

  • 1 <= s.length <= 100

  • s contains only digits and may contain leading zero(s).

Pattern

String, Dynamic Programming

Approaches

Explanation

Let num_ways[i] be the number of ways to decode s[:i]. We can decode the last number as a letter or the last 2 number as a letter. Assuming both decodings are possible,

num_ways[i] = num_ways[i - 1] + num_ways[i - 1]

However, we cannot always decode the last number or last 2 number. The last 2 number is obvious—e.g. “45”—but “0” is not a valid 1 number code. If the respective 1/2 number combination is valid, we replace num_ways with 0.

CODES = {str(i) for i in range(1, 27)}
one_digit = num_ways[i - 1] if s[i] in CODES else 0
two_digit = num_ways[i - 2] if s[i - 1 : i + 1] in CODES else 0
num_ways[i] = one_digit + two_digit

The total number of ways to decode s is then num_ways[-1].

Code

CODES = {str(i) for i in range(1, 27)}


def numDecodings(s: str) -> int:
    """Find the number of ways to decode a string of integers as letters."""
    N = len(s)
    num_ways = [0] * N
    num_ways[0] = 1 if s[0] in CODES else 0

    for i in range(1, N):
        if i == 1:
            one_digit = num_ways[i - 1] if s[i] in CODES else 0
            two_digit = 1 if s[i - 1 : i + 1] in CODES else 0
            num_ways[i] = one_digit + two_digit
        else:
            one_digit = num_ways[i - 1] if s[i] in CODES else 0
            two_digit = num_ways[i - 2] if s[i - 1 : i + 1] in CODES else 0
            num_ways[i] = one_digit + two_digit

    return num_ways[-1]

Test

>>> from decode_ways__dynamic_programming import numDecodings
>>> numDecodings("12")
2
>>> numDecodings("226")
3
>>> numDecodings("06")
0

Complexity

\(n\) is the length of s

Measure

Complexity

Notes

Time

\(O(n)\)

one pass through s

Auxiliary Space

\(O(n)\)

num_ways array

decode_ways__dynamic_programming.numDecodings(s: str) int

Find the number of ways to decode a string of integers as letters.