/**
 * @param {string[]} strings
 * @return {string[][]}
 */
var groupStrings = function(strings) {
  var dicts = {};
  var gs = [];

  for (var i = 0; i < strings.length; i ++) {
    var val = strings[i];
    var key = getKey(val);

    if (dicts[key]) {
      dicts[key].push(val);
    } else {
      gs.push([val]);
      dicts[key] = gs[gs.length - 1];
    }
  }

  return gs;
};

/**
 * serialize `str` to get it start with `a`
 * */
function getKey(str) {
  if (str.charAt(0) == 'a') return str;

  // ascii code for a
  var charA = 'a'.charCodeAt(0);
  // ascii code for z
  var charZ = 'z'.charCodeAt(0);

  var distance = str.charCodeAt(0) - charA;
  var arr = str.split('');
  for (var i = 0; i < arr.length; i ++) {
    var val = arr[i].charCodeAt(0);

    if (val - distance < charA) {
      val = charZ + 1 - (charA - (val - distance));
    } else {
       val = val - distance;
    }

    arr[i] = String.fromCharCode(val);
  }

  return arr.join('');
}


var eq = require('assert').deepEqual;

// @note this problem doesn't require the order of groups
eq(groupStrings(['abc', 'bcd', 'acef', 'xyz', 'az', 'ba', 'a', 'z']), [
  ['abc','bcd','xyz'],
  ['acef'],
  ['az','ba'],
  ['a','z']
]);


@@ -383,219 +383,187 @@
 ;%0A%0A/
-**%0A * serialize %60str%60
+/  encode string with distance of
  t
+w
 o 
-get it start with %60a%60%0A * */%0Afunction getKey(str) %7B%0A  if (str.charAt(0) == 'a') return str
+adjacent charactors,%0A//  return %60-11-2%60 etc%0Afunction getKey(str) %7B%0A  var key = ''
 ;%0A
-%0A
   
-// ascii code for a%0A  var charA = 'a'.charCodeAt(0);%0A  // ascii code for z%0A  var charZ = 'z'
+for(var i = 1; i %3C str.length; i ++) %7B%0A    var val = str
 .cha
@@ -574,27 +574,26 @@
 eAt(
-0);%0A%0A
+i);%0A  
   var 
-distance
+preVal
  = s
@@ -610,298 +610,191 @@
 eAt(
-0) - charA;%0A  var arr = str.split(''
+i - i
 );%0A
+%0A
   
-for (
+  
 var 
-i = 0; i %3C arr.length; i ++) %7B%0A    var val = arr%5Bi%5D.charCodeAt(0);%0A%0A    if (val - distance %3C charA) %7B%0A      val = charZ + 1 - (charA - (val - distance));%0A    %7D else %7B%0A       val = val - distance;%0A    %7D%0A%0A    arr%5Bi%5D = String.fromCharCode(val)
+d = val - preVal;%0A    if (val %3C preVal) %7B%0A      d = val - preVal + 26;%0A    %7D%0A%0A    // distance could be %6011%60, need to distinguish with %60'1'%60 + %60'1'%60%0A    key += '-' + d
 ;%0A  
@@ -809,26 +809,16 @@
 urn 
-arr.join('')
+key
 ;%0A%7D%0A%0A
-%0A
 var 
@@ -1045,12 +1045,71 @@
 ','z'%5D%0A%5D);%0A%0A
+eq(groupStrings(%5B'abc','am'%5D), %5B%0A   %5B'abc'%5D,%0A   %5B'am'%5D%0A%5D);%0A

change the method of encoding string