Search code examples
c++algorithmdictionaryhashtablehash-collision

Hash table for search words in large text. O(1)


I must create hash table with operation speed O(1) for large text(search, paste, delete). Is it real? How minimize collision? Any example? I never used C++ later. i can't find any example hash table with dictionary for text.
Target language C++ (only STL).


Solution

  • You can make use of unordered_map or unordered_set that are part of C++11 standard, or use boost's version of these containers if C++11 is not an option. If you need to implement the solution on your own I believe you can use the implementation of either as reference.

    EDIT: as pointer out by amit this is still not constant as you need to hash a string, thus you need to traverse it at least once. Thus the complexity is O(|S|) where S is the string being searched. Also the complexity is expected O(|S|) as no matter how good the hash function collisions may occur(and except of the very rare case where a perfect hash can be used it will with very, very high probability due to the birthday paradox).