← Whiteboard questions

Decode Ways

Count the ways to decode a digit string, 1 is A to 26 is Z

DSA, Medium, 1-D DP

Which structure or algorithm, and what does it cost.

A sensor sends a string s of digits. Cut s from left to right into pieces. Each piece stands for a letter (1 is A, 26 is Z), so it must be a whole number from 1 to 26 written without a leading zero, so 7 and 26 are fine but 0 and 05 are not. Return how many different ways there are to cut s. Return 0 if no cut works.

Examples

Input s = "2611"
Output 4
Why the cuts are 2|6|1|1, 26|1|1, 2|6|11 and 26|11.
Input s = "301"
Output 0
Why 30 is above 26 and a lone 0 is not allowed, so the 3 and the 0 cannot be placed.
Input s = "1010"
Output 1
Why edge case: each 0 must follow a 1, so the only cut is 10|10.

Constraints

Your answer must contain

Target Aim for O(n) time and O(1) memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Look at the last piece of a cut. How many digits can it have, and which of those choices are allowed?

Hint 2 the approach

Count the cuts of the first k digits, for each k. How could that count depend on counts for smaller k?

Hint 3 the key step

A one-digit last piece, not 0, adds the count for k - 1. A two-digit last piece, from 10 to 26, adds the count for k - 2.

Walkthrough

Full walkthrough brute force to best

Brute force

Try every cut with recursion: at the front take one digit, or two digits when they form 10 to 26, then cut the rest the same way. The same suffixes are solved again and again. For a string of 70 ones, the calls number over 10^14.

Better method

Memoised recursion. Let ways(i) count the cuts of the digits from position i to the end. Save each answer, so every position is solved once. The time is O(n), the memory is O(n) and the recursion is n levels deep.

Best method

Let ways[k] be the number of cuts of the first k digits, with ways[0] = 1 for the empty start. The last piece of a cut of the first k digits is either one digit or two. A one-digit piece is allowed when the digit is not 0, and then the digits before it can be cut in ways[k - 1] ways. A two-digit piece is allowed when its value is from 10 to 26, and then the digits before it can be cut in ways[k - 2] ways. Add the cases that apply. The two cases never overlap, because the pieces differ in length. Each count needs only the two before it, so keep two numbers. The answer is ways[n].

Hand trace

Run on 2611. Let ways[k] be the number of cuts of the first k digits. ways[0] = 1 and ways[1] = 1, because the digit 2 is allowed. For k = 2, the last digit 6 is allowed, so add ways[1] = 1, and the pair 26 is allowed, so add ways[0] = 1. That gives ways[2] = 2. For k = 3, the last digit 1 is allowed, so add ways[2] = 2, and the pair 61 is above 26, so add nothing. That gives ways[3] = 2. For k = 4, the last digit 1 adds ways[3] = 2, and the pair 11 adds ways[2] = 2. That gives ways[4] = 4. The answer is 4.

Code

def count_splits(s):
    prev, cur = 1, 1 if s[0] != '0' else 0      # cuts of the first 0 digits and of the first 1 digit
    for i in range(2, len(s) + 1):
        nxt = 0
        if s[i - 1] != '0':
            nxt += cur                          # the last piece is one digit
        if 10 <= int(s[i - 2:i]) <= 26:
            nxt += prev                         # the last piece is two digits
        prev, cur = cur, nxt
    return cur

Time and memory

O(n) time, because each digit is handled once. O(1) memory, because only two counts are kept.

Common mistakes

  • Treating 0 as a normal digit. A lone 0 is not a piece, and 0 can only be the second digit of 10 or 20. So 301 gives 0.
  • Allowing a two-digit piece that starts with 0, such as 05. Check that the value is from 10 to 26, not only that it is at most 26.
  • Using plain recursion with no saved answers. A string of 70 ones needs over 10^14 calls.

Related problems

More 1-D DP problems
Decode Ways: hints, walkthrough and Python solution