Skip to content
Efficient

Collision Handling

Dealing with hash collisions in string matching.

Time Complexity
O(1)
Space Complexity
O(1)

Interactive Visualization

Chaining (Linked Lists)
0
1
"cat"
"act"
2
Open Addressing
0: empty
1: "busy"
2: "collision_probed"
3: empty

Technical Guide

In hash-based string algorithms, different strings can produce the same hash value (a collision). We handle this through Chaining (storing colliding items in a list) or Open Addressing (finding the next empty slot). In Rabin-Karp, we handle collisions by simply verifying the characters when a hash match occurs.

Logical Phases

  • 1
    Detecting when Hash(A) == Hash(B) but A != B.
  • 2
    Chaining: Using a linked list at the hash bucket.
  • 3
    Linear Probing: Searching for the next available index.
  • 4
    Double Hashing: Using a second hash function to find a new index.

Implementation

if (h1 == h2 && text.slice(i, i+m) === pattern) match();

Expert Pitfalls

A high collision rate can degrade Rabin-Karp's O(N) performance back to O(N*M).