Every Letter Combination From a Phone Keypad Sequence
Implement keypadLetterCombinations
Given a string of digits '2' through '9', where each digit maps to a fixed set of letters exactly like an old phone keypad, produce every possible letter combination the digit sequence could represent. Each digit contributes one letter to each combination, in order.
Keeping a running list of every combination built so far and rebuilding it from scratch at every digit works, but re-copies every prefix's characters into a fresh string each time a new digit is layered on. A single shared buffer that gets one character appended, recursed past, and popped back off avoids that entirely — the same prefix is never rebuilt, only briefly extended and then restored.
Example 1:
Input: digits = "68"
Output: ["mt","mu","mv","nt","nu","nv","ot","ou","ov"]
Example 2:
Input: digits = "4"
Output: ["g","h","i"]
Example 3:
Input: digits = ""
Output: []
+ 4 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ digits.length ≤ 6 - ●
digits[i] is one of '2'–'9', using the standard phone keypad letter mapping (2=abc, 3=def, 4=ghi, 5=jkl, 6=mno, 7=pqrs, 8=tuv, 9=wxyz) - ●
An empty input returns an empty list of combinations - ●
Results are returned sorted in ascending (lexicographic) order for a stable, checkable answer
digits =
68