美文网首页
249. Group Shifted Strings

249. Group Shifted Strings

作者: jluemmmm | 来源:发表于2021-10-18 10:34 被阅读0次

分组旋转字符串

  • 时间复杂度O(mn),空间复杂度O(mn)
  • Runtime: 80 ms, faster than 71.19%
  • Memory Usage: 41.5 MB, less than 18.64%
/**
 * @param {string[]} strings
 * @return {string[][]}
 */
var groupStrings = function(strings) {
  if (!strings || !strings.length) {
    return [];
  }
  const map = new Map();
  for (let val of strings) {
    let res = [];
    for (let i = 1; i < val.length; i++) {
      let diff = val.charCodeAt(i) - val.charCodeAt(i - 1);
      diff = diff < 0 ? (diff + 26) : diff;
      res.push(diff);
    }
    const key = res.join('-');
    const arr = map.has(key) ? map.get(key) : [];
    arr.push(val);
    map.set(key, arr);
  }
  return Array.from(map.values());
};
``

相关文章

网友评论

      本文标题:249. Group Shifted Strings

      本文链接:https://www.haomeiwen.com/subject/pfogoltx.html