we have a digit letter map that looks like this
const digitsLetters = new Map([
["2", ['a', 'b', 'c']],
["3", ['d', 'e', 'f']],
["4", ['g', 'h', 'i']],
["5", ['j', 'k', 'l']],
["6", ['m', 'n', 'o']],
["7", ['p', 'q', 'r', 's']],
["8", ['t', 'u', 'v']],
["9", ['w', 'x', 'y', 'z']],
]);
The question asks us to return the all possible letter combinations that the number could represent. For example, if we have "23"
then first digit 2
maps to ['a', 'b', 'c']
and second digit maps to ['d', 'e', 'f']
and we end up getting ["ad","ae","af","bd","be","bf","cd","ce","cf"]
.
I have found a way to produce such a combination between two arrays.
// this will output ["ad","ae","af","bd","be","bf","cd","ce","cf"]
['a', 'b', 'c'].map(char1 => ['d', 'e', 'f'].map(char2 => char1 + char2)).flat(2)
So I thought I could just recursively apply this algorithm for such digit until I hit the last one. I think it is doable. However I had a hard time implementing the solution. Can someone please help me?