Count Decodings of a Digit String
Updated
Problem statement
A message of capital letters was encoded by replacing each letter with its position in the alphabet: A becomes 1, B becomes 2, and so on up to Z, which becomes 26. The numbers were then written one after another with no separators.
Given the digit string digits, return the number of ways to split it into pieces so that every piece is a number from 1 to 26 written without a leading zero. Each such split decodes to one message. If no split works, return 0.
The count can be very large, so return it modulo 10^9 + 7.
Function
countDecodings(digits: String) → intExamples
Example 1
digits = "2611"return = 4The splits are 2 6 1 1 (BFAA), 26 1 1 (ZAA), 2 6 11 (BFK) and 26 11 (ZK). The middle 6 and 1 cannot form one piece, because 61 is larger than 26.
Example 2
digits = "1020"return = 1A 0 cannot be a piece on its own, so each 0 must join the digit before it. The only split is 10 20, which decodes to JT.
Example 3
digits = "804"return = 0The 0 cannot stand alone, and joined to the 8 it makes 80, which is larger than 26. No split works, so the answer is 0.
Constraints
1 <= digits.length <= 10^4digitsconsists only of the characters0to9.
More Bank of America Online Assessment questions
- Return Exact Cash Register ChangeOA · Seen Aug 2026
- Sum Skipping Multiples of 7OA · Seen Nov 2025
- Wildcard Substring MatchOA · Seen Nov 2025
- Maximum String BeautyOA · Seen Sep 2024
- Merge Point of Two Linked ListsOA · Seen Oct 2023
- Remove Duplicates from a ListOA · Seen Oct 2023
- Pairs with Target Sum in a Sorted ArrayOA · Seen Oct 2023
- Count Level-Order Traversals of a TreeOA · Seen Sep 2023
- Count Set BitsOA · Seen Sep 2023
- Count Pairs with Sum KOA · Seen Jun 2023
Similar questions at other companies
- Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
- Longest Subsequence That Is a SubstringBooking.com · OA
- Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OAMicrosoft interview questions
- Palindrome CounterGoldman Sachs · OAGoldman Sachs interview questions
- Longest String ChainVisa · OAVisa interview questions
- Minimum-Cost Digit String DecodingSquadStack.ai · OA
- Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
- Substrings Without a Target SubsequencePhonePe · OAPhonePe interview questions
- Minimum Edit-Distance String for Each QueryTarget · OA
- Longest Subsequence That Is a SubstringWells Fargo · OAWells Fargo interview questions