Given a string containing only the numbers 2-9, return all the letter combinations it can represent. The answers can be returned in any order.

The mapping of numbers to letters is given as follows (same as a telephone key). Note that 1 does not correspond to any letter.

For specific ideas, please refer to the video:

www.bilibili.com/video/BV1FK…

Code implementation:

let letterArr = [
    ' ',
    '',
    'abc',
    'edf',
    'ghi',
    'jkl',
    'mno',
    'pqrs',
    'tuv',
    'wxyz'
]
let results = []
var letterCombinations = function(digits) {
    if(digits.length === 0) {
        return results
    }
    findCombination(digits, 0, '')
    return results
};
function findCombination(digits, index, str) {
    if(index === digits.length) {
        results.push(str)
        return
    }
    let c = digits[index]
    let letters = letterArr[c - '0']
    for (let i = 0; i < letters.length; i++) {
        findCombination(digits, index + 1, str + letters[i])
    }
}
console.log(letterCombinations(''))
Copy the code