Computer Science, asked by NANDHINI3005, 2 months ago

algorithm to write which words will be saved in the dictionary​

Answers

Answered by jha85841
6

Explanation:

Step 1:Create an array of prime numbers.

int primes[] = {2, 3, 5, 7, ...};

We are using prime number to avoid false collisions.

Step 2:Create a method to calculate hash code of a word\string.

int getHashCode(String str){

int hash = 31;

for(i =0 to length of str){

hash = hash*primes['a' - str.charAt[i]];

}

return hash;

}

Step 3: Now store all words in a HashMap.

void loadDictionary(String[] words){

for( word from words for i = 0 to length of words) {

int hash = getHashCode(word);

List<String> anagrams = dictionary.get(hash);

if(anagrams ! = null){

anagrams.add(word);

} else

List<String> newAnagrams = new ArrayList<String>();

newAnagrams.add(word);

dictionary.put(hash, newAnagrams);

}

}

}

Step 4: Now here is the approach to find anagrams:

int findNumberOfAnagrams(String str){

List<String> anagrams = dictionary.get(getHashCode(str));

return anagrams.size();

}

Answered by sirishachinni38
0

Answer:

cat Batman latt matter cat matter cat

Similar questions