Group Shifted Strings
Description
null
Solution(javascript)
/**
* @param {string[]} strings
* @return {string[][]}
*/
var groupStrings = function(strings) {
const getHash = (str = "") => {
const base = str.charCodeAt(0)
let hash = "0"
for(let i = 1; i < str.length; i++) {
let diff = str.charCodeAt(i) - base
if(diff < 0) {
diff += 26
}
hash = hash + "-" + diff
}
return hash
}
const map = strings.reduce((acc, str) => {
const hash = getHash(str)
acc[hash] = acc[hash] || []
acc[hash].push(str)
return acc
}, {})
return Object.values(map)
};