Find Number of Different Words That Can Be Formed
Updated
Problem statement
Given a string representing a sequence of digits, find the number of different words that can be formed by interpreting the digits as indices in the alphabet where '1' corresponds to 'a', '2' to 'b', and so on, up to '26' for 'z'.
A valid word is formed by splitting the string into one or two-digit numbers and then mapping those numbers to their corresponding letters. The number '0' does not correspond to any letter, and numbers greater than '26' do not correspond to any letter.
Function
numDifferentWords(s: String) → int
Complete the function numDifferentWords in the editor.
numDifferentWords has the following parameter:
String s: the string representing the sequence of digits
Returns int: the number of different words that can be formed
Examples
Example 1
s = "1192"return = 3The following different words can be formed from the string "1192":
- 1,1,9,2 - aaib
- 11,9,2 - kib
- 1,19,2 - asb
Therefore, the answer is 3.
More Goldman Sachs interview questions
- House Robber IIIPHONE SCREEN · Seen Nov 2026
- Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
- Maximum Ones After K OperationsOA · Seen Nov 2026
- Palindrome CounterOA · Seen Oct 2026
- Longest Palindromic SubsequenceOA · Seen Oct 2026
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Meeting Rooms Without a Sweep LineONSITE INTERVIEW · Seen Oct 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 · OA
- Longest String ChainVisa · OA
- Minimum-Cost Digit String DecodingSquadStack.ai · OA
- Maximum Redundancy After One Segment FlipBloomberg LP · OA
- Substrings Without a Target SubsequencePhonePe · OA
- Minimum Edit-Distance String for Each QueryTarget · OA
- Longest Subsequence That Is a SubstringWells Fargo · OA
- Non-Alternating Binary PartitionsThe D.E. Shaw Group · OA