Created
November 4, 2020 21:03
-
-
Save dabsclement/e5d20dab606b9abc9ee9fd6de068c19b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} digits | |
* @return {string[]} | |
*/ | |
const letterCombinations = function(digits) { | |
if (digits.length === 0) return []; | |
const 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"] | |
}; | |
let combinations = ['']; | |
for (let i = 0; i < digits.length; i++) { | |
let digit = digits[i]; | |
let letters = map[digit]; | |
let tempArray = []; | |
if (letters === undefined) continue; | |
for (let j = 0; j < letters.length; j++) { | |
let letterToAdd = letters[j]; | |
for (let k = 0; k < combinations.length; k++) { | |
let combination = combinations[k]; | |
tempArray.push(combination + letterToAdd); | |
} | |
} | |
combinations = tempArray; | |
} | |
return combinations; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment