Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Ways to read a digit message

    MediumAbout 25 minFull AccessLearn it: How to recognize a DP problem in the wild

    The task

    An old pager sends letters as numbers, A = 1, B = 2, ..., Z = 26, with no separators, so one string of digits can stand for several messages.

    Complete DecodeWays(string digits). Return how many letter strings the digits can stand for. Each letter uses one digit or two: a 0 cannot stand alone, and a two-digit code cannot start with 0 (so "05" is not a letter) or be larger than 26. Throw ArgumentException for an empty string or for any character that is not a digit. The answer fits in a long.

    Signature

    public static long DecodeWays(string digits)

    Examples

    • "123" → 3. 1-2-3, 12-3 and 1-23.
    • "08" → 0. 0 cannot stand alone, and 08 is not a code.

    Limits: Up to 90 digits.

    The tests check: a lone 0, a code starting with 0, 0 inside, two zeros in a row, 27 and above, a message of one and of many readings, an empty message, a character that is not a digit, and sixty 1s in time, a count beyond int.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // How many letter strings (A = 1 ... Z = 26) the digits can stand for.
          public static long DecodeWays(string digits)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.