Letter Combinations of a Phone Number — javascript solution


  • 0
    W

    var obj = {
    0: ' ',
    1: '',
    2: 'abc',
    3: 'def',
    4: 'ghi',
    5: 'jkl',
    6: 'mno',
    7: 'pqrs',
    8: 'tuv',
    9: 'wxyz'
    };
    var letterCombinations = function(digits) {
    var arr = [[]];
    var reg = /[\s]/g;
    digits = digits.replace(/\s/g, '');
    if(digits.length === 0){
    return [];
    }
    if(digits.length === 1){
    return obj[digits[0]].split("");
    }
    for(var i = 0; i < digits.length; i++){
    arr[0][i] = obj[digits[i]].split("");
    }
    for(var j = 1; j == 1;){
    arr[0].unshift([]);
    arr[0][j].map(function(item, index1){
    arr[0][j+1].map(function(item, index){
    arr[0][0].push(arr[0][j][index1] + arr[0][j+1][index]);
    });
    });
    arr[0].splice(1, 1);
    arr[0].splice(1, 1);
    if(arr[0].length === 1){
    return arr[0][0];
    }
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.