249. Group Shifted Strings
2021-10-18 本文已影响0人
jluemmmm
分组旋转字符串
- 时间复杂度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());
};
``