[LeetCode] Letter Combinations of a Phone Number (Java)

Given a digit string, 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:
Although the above answer is in lexicographical order, your answer could be in any order you want.

Analysis

It’s a easy question. But we can do it both in recursive way and iterative way.

In the iterative way, we use the result from the previous digits, and then add new letters to them.

Code

Recursive version

 Iterative version

 Complexity

Assuming the average number of letters on every number is m, and the length of digits string is n, then the complexity is O(mn).