Javascript solution


  • 0
    Y
    var groupStrings = function(strings) {
    var letter = 'abcdefghijklmnopqrstuvwxyz'.split('');
    var dic = {};
    var result = [];
    strings.sort();
    strings.forEach(function(str) {
        var key = transfer(str, letter);
        if(key in dic) {
            dic[key].push(str);
        }
        else {
            dic[key] = [str];
        }
    });
    for(var key in dic) {
        if(dic.hasOwnProperty(key)) result.push(dic[key]);
    }
    return result;
    

    };

    function transfer(str, letter) {
    var result = '';
    if(!str.length) return result;
    var gap = str.charCodeAt(0)-97;
    result = 'a';
    for(var i = 1; i < str.length; ++i) {
        var index = str.charCodeAt(i)-97;
        index-=gap;
        if(index < 0) index+=26;
        result+=letter[index];
    }
    return result;
    

    }


Log in to reply
 

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