C++ I am looking for a hashing algorithm that gives me only couple of collisions
ID: 661611 • Letter: C
Question
C++
I am looking for a hashing algorithm that gives me only couple of collisions in a table size less than 400 with about 102 dictionary words.
The hash algorithm must return the same value for a string regardless of its character placement, as an example it must return the same value for book and koob .
I tried adding the ascii values and multiply by a prime number % table size but it does have many collisions.
Please help .
My hashing algorithm
int hash(string str){
int sum = 0;
for(int i = 0; i < str.length(); i++){
sum += (str[i] - '0');
}
sum += str.length();
return (sum * 31) % SIZE;
}
Explanation / Answer
Modified code:
//Method definition of hash function
int hash(string str)
{
//Initialize variable "sum" value is 1
int sum = 1;
//return the same value for a string
for(int i = 0; i < str.length(); i++)
sum *= str[i];
return sum;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.