I am new to programming and hash data structure. I was trying to solve this problem. It works when I used an Object as the codes showed below. But it fails to work when I use a Map. Could someone kindly tell me what went wrong with my map? Many many thanks!
//with object:
var groupStrings = function(strings) {
const res = {};
for(let s of strings){
const key = new Array(s.length).fill(0);
for(let i=1;i<s.length;i++){
key[i]=(s.charCodeAt(i)-s.charCodeAt(i-1)+26)%26;
}
if(res[key]){
res[key].push(s)
}
else res[key]=[s] ;
}
return Object.values(res);
};
//The one used map and didn't work.
/**
* @param {string[]} strings
* @return {string[][]}
*/
var groupStrings = function(strings) {
const map1 = new Map();
for(let s of strings){
const key = new Array(s.length).fill(0);
for(let i=1;i<s.length;i++){
key[i]=(s.charCodeAt(i)-s.charCodeAt(i-1)+26)%26;
}
if(map1.has(key)){map1.get(key).push([s])}
else map1.set(key,[s]) ;
}
return [...map1.values()];
};