On an old phone keypad, each key from 2 to 9 carries a few letters. To type a word you press the key that holds each letter, so one string of key presses can stand for many different words.
In full: 2 → abc, 3 → def, 4 → ghi, 5 → jkl, 6 → mno, 7 → pqrs, 8 → tuv, 9 → wxyz.
You get the key presses as a string digits. Return every letter string they could spell: one letter from each pressed key, in the order the keys were pressed. You may return the strings in any order.
Key 5 offers j, k or l, and key 8 offers t, u or v: 3 × 3 = 9 strings.
Keys 7 and 9 carry four letters each.
Constraints
1 <= digits.length <= 4digits[i] is one of '2' to '9'.Each key multiplies the choices: every string so far gets extended by each letter on the next key.
Each key press is an independent choice of one letter, and the word is those choices read in order. So the answers are every way to pick a letter from the first key, then one from the second, and so on: a product of choices. "58" gives 3 × 3 = 9 words, and four presses of 9 give 4⁴ = 256.
Time O(4ⁿ · n)Space O(n) besides the answer
The full walkthrough and code are part of Premium