Javascript, 260ms, backtracking


  • 0
    Y



    var generateAbbreviations = function(word) {
    var result = [];
    if(!word) return [''];
    var sb = [];
    dfs(result, sb, word, 0, 0);
    return result;
    };
    function dfs(result, sb, strs, num, index) {
    if(index === strs.length) {
    result.push(sb.join('')+(num?String(num):''));
    return;
    }
    dfs(result, sb, strs, num+1, index+1);
    if(num)sb.push(num);
    sb.push(strs.charAt(index));
    dfs(result, sb, strs, 0, index+1);
    sb.pop();
    if(num)sb.pop();
    }


Log in to reply
 

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