Hash function h+=(A[i]*A[i]) %mod


#1

#define mod 1000007
int hsh(string s){
int h=0;
for(int i=0;i<s.size();i++){
h=(h+(s[i]-‘a’)*(s[i]-‘a’))%mod;
}
return h;
}

vector<vector > Solution::anagrams(const vector &A) {
unordered_map<int,vector> m;
vector<vector> ans;
for(int i=0;i<A.size();i++){
m[hsh(A[i])].push_back(i+1);
}
for(auto x:m){
ans.push_back(x.second);
}
return ans;

}


#2

Thanks, it was really helpful I was doing the sum of characters as hash


#3

How did you approached to this hash function?


#4

Your code will give wrong output for testcase [“a”,“ab”].
Your function return hash values for both string 1