Strings

Problem shape

Intermediate
4
8
15
16
23
42
Core idea

Compare rolling hashes of fixed-size windows against the pattern hash.

Step 1 / 5
Problem shape
Current lesson

Rabin-Karp Lab

Strings Intermediate

Rabin-Karp

Welcome to the Rabin-Karp algorithm lab. Compare rolling hashes of fixed-size windows against the pattern hash. Use it when: Use for multiple pattern checks or rolling hash practice. The time complexity is Average O(n + m), and the space complexity is O(1). The pseudocode is: patternHash = hash(pattern) windowHash = first window for each window if hashes match, verify characters roll hash to next window As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

Average O(n + m)

Space

O(1)

Pseudocode

patternHash = hash(pattern)
windowHash = first window
for each window
  if hashes match, verify characters
  roll hash to next window
Open deeper structure lesson
1.0x