suppose we are to implement speller using an array instead of a hash-table or a trie.
a main problem that we have is that we don't know the number of words in the dictionary in advance (yes, we know the number of words in the given dictionary per the specs, but our program should allow loading custom dictionaries).
as a result, we would have to dynamically go through the headache of dynamically allocating memory for our array.
but what's bad about that? aren't we dynamically allocating memory for our hash-table or trie anyway?
well, yes, but every time we are inserting a new word/character in our dictionary, we are just allocating memory for this word/character, forgetting about the ones we already inserted before, while in case of an array, we would have to keep track of the current number of elements in our array and use realloc
to reallocate, in our case, a bigger block of memory when, for example, half of the capacity of our array is reached.
problem with realloc
is that, after managing to allocate a new block of memory, it may copy all the contents from the original block into the new block so that the contents remain unchanged and all of this is of course costing in terms of time and memory space.
so why not set a maximum size for our array?
technically, while it may work for some cases, I wouldn't consider it a correct solution as there is no where in the specs that specifies a maximum size for our dictionary.