← problems
#91

Decode Ways

MediumDynamic Programming

A message of digits is encoded to letters with A -> 1, B -> 2, ..., Z -> 26. Given a string s of digits, return the number of ways to decode it.

A leading 0 (or a 0 that can't pair into 10..26) yields no valid decoding for that piece.

Example

"12"   => 2   // "AB" or "L"
"226"  => 3   // "BZ", "VF", "BBF"
"06"   => 0
Implement
function NumDecodings(s: string) -> int

Found a bug? Or annoyed about something in the language? Tell us.

loading editor…