1

I want to use a hash table to store words. For example, I have two words aba and aab ,because they are made up of the same elements just in different order , so I want to store them with the same index, and plug a link list at that link list. It's easy for me to search in a certain way. The elements of words are just 26 letters. How to design a proper index of the hash table? How to organize the table?

wildplasser
  • 38,231
  • 6
  • 56
  • 94

1 Answers1

0

So the questions you want to answer with your hash table is: What words can be built with the letters I have?

I assume you are reading some dictionary and want to put all the values in the hash table. Then you could use an int array with the count how many times each letter occurs as key (e.g. 'a' would be index 0 and 'z' index 25) and for the value you would have to use a list, so that you can add more than one word to that entry.

But the simplest solution is probably just to use the sorted word as key (e.g. 'aba' gets key 'aab' and 'aab' obviously too), because words are not very long the sort isn't expensive (avoid creating new strings all the time by working with the character array).

So in Java you could get the key like this:

char[] key = word.toCharArray();
Arrays.sort(key);
// and if you want a string
String myKey = new String(key);
maraca
  • 7,323
  • 3
  • 20
  • 41