17_Letter Combinations of a Phone number
Last updated
Last updated
[Medium]
Given a string containing digits from2-9
inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Note: Although the above answer is in lexicographical order, your answer could be in any order you want.
Idea:
Two passes. In the first pass, compute the length of the final output. In the second pass, append letters for each element.
Use an index which is at the same length as the final output to indicate which letter should we write. Divide the product of the letter length of all the future digits, the quotient indicates the index of current letter, the modulo indicates index for next round.
Time Complexity: where n is the length of input.
Space Complexity: where n is the length of input.