Count Complete Digit-to-Letter Decodings

Quick Overview

Count complete digit-to-letter decodings with valid zero handling and safe intermediate arithmetic under a final signed32 answer guarantee.

Count Complete Digit-to-Letter Decodings

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem The codes `"1"` through `"26"` represent the letters A through Z. Given a digit string `s`, count how many ways its entire sequence can be divided into valid one- or two-digit codes. A code cannot begin with zero: `"6"` is valid, `"06"` is not, and `"0"` has no letter. Return 0 if there is no complete decoding. The input cases guarantee that the final answer fits a signed 32-bit integer. ```cpp int numDecodings(string s); ``` ### Examples ```text Input: s = "121" Output: 3 ``` The groupings are `1|2|1`, `12|1`, and `1|21`. ```text Input: s = "100" Output: 0 ``` Neither the final zero on its own nor `00` is a valid code. ### Constraints - `1 <= s.length <= 100`. - `s` contains digits only and may start with zeros. - The final number of complete decodings is at most 2147483647.

Overview: Count complete digit-to-letter decodings with valid zero handling and safe intermediate arithmetic under a final signed32 answer guarantee.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
2
0

Problem

The codes "1" through "26" represent the letters A through Z. Given a digit string s, count how many ways its entire sequence can be divided into valid one- or two-digit codes. A code cannot begin with zero: "6" is valid, "06" is not, and "0" has no letter.

Return 0 if there is no complete decoding. The input cases guarantee that the final answer fits a signed 32-bit integer.

int numDecodings(string s);

Examples

Input: s = "121"
Output: 3

The groupings are 1|2|1, 12|1, and 1|21.

Input: s = "100"
Output: 0

Neither the final zero on its own nor 00 is a valid code.

Constraints

  • 1 <= s.length <= 100 .
  • s contains digits only and may start with zeros.
  • The final number of complete decodings is at most 2147483647.