Letter Combinations of a Phone Number – Solution & Complexity
1. Understand the Search Space
- Each digit maps to a small group of letters, like
2 -> abcand3 -> def. - A valid answer chooses exactly one letter for every digit.
- If
digitsis empty, there are no combinations to build, so return an empty list.
2. Start with Incremental Building
- Keep a list of partial combinations seen so far.
- For the next digit, append each possible letter to each partial combination.
- This works, but recursion or backtracking expresses the choice process more directly.
3. Use Backtracking
- Backtracking builds one candidate combination at a time.
- At position
i, choose one letter fordigits[i], recurse to the next position, then try the next letter. - When the candidate length equals the input length, it is complete.
4. Preserve the Required Order
- Iterate through digits from left to right.
- Iterate through each mapped letter in the normal phone keypad order.
- This naturally produces results like
ad, ae, af, bd, ...for23.
5. Complete Solution and Complexity
- The final solution handles the empty input first, then backtracks through every possible letter choice.
- If there are
ndigits and each has up to 4 letters, time complexity is O(4^n * n) including string creation. - Space complexity is O(n) for the recursion path, plus the output list.