Problem · String

Decode Numeric String

Learn this problem
MediumArcesiumFULLTIMEONSITE INTERVIEW

Problem statement

A message is encoded by mapping letters to numbers: A -> 1, B -> 2, ..., Z -> 26.

Given a numeric string digits, return the number of different ways it can be decoded into letters.

A one-digit code is valid when it is between 1 and 9. A two-digit code is valid when it is between 10 and 26.

Function

countDecodings(digits: String) → int

Examples

Example 1

digits = "2116"return = 5

The valid decodings split the string as 2,1,1,6, 21,1,6, 2,11,6, 2,1,16, and 21,16.

Constraints

  • 1 <= digits.length <= 10^5
  • digits consists of numeric characters.
  • The number of valid decodings is guaranteed to fit in a signed 32-bit integer.

More Arcesium problems

drafts saved locally
public int countDecodings(String digits) {
  // write your code here
}
digits"2116"
expected5
checking account