Decode Ways
Updated
Problem statement
Map the numbers 1 through 26 to the letters A through Z. Return how many ways the entire string digits can be split into valid one-digit or two-digit numbers under this mapping.
A piece cannot begin with zero. For example, 10 and 20 are valid pieces, but 0, 06, and 30 are not.
Function
numDecodings(digits: String) → intExamples
Example 1
digits = "12"return = 2The valid partitions are 1|2 and 12, corresponding to AB and L.
Example 2
digits = "226"return = 3The valid partitions are 2|2|6, 22|6, and 2|26, corresponding to BBF, VF, and BZ.
Example 3
digits = "06"return = 0The leading zero cannot form a valid piece. In particular, 06 is not the same code as 6.
Constraints
1 <= digits.length <= 100digits contains only ASCII decimal digits 0 through 9.The number of valid decodings is at most 2^31 - 1.
More JP Morgan Chase Online Assessment questions
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
- Task SchedulingOA · Seen Oct 2026
- Substring RemovalOA · Seen Oct 2026
- Maximum Game Board ScoreOA · Seen Sep 2026
- Minimum Digit Changes Between String HalvesOA · Seen Sep 2026
- Same Substring Within BudgetOA · Seen Sep 2026
- Invert Ad Visibility BitsOA · Seen Aug 2026
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