0
0
DSA Pythonprogramming~10 mins

Rabin Karp String Matching in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Rabin Karp String Matching
Start
Compute pattern hash
Compute initial text window hash
For each text window
Compare hashes
Check exact chars
Slide window by 1
Repeat until end of text
End
The algorithm computes a hash for the pattern and for each window of text. It compares hashes first, then characters if hashes match, sliding the window until the end.
Execution Sample
DSA Python
def rabin_karp(text, pattern):
    p_len = len(pattern)
    p_hash = hash(pattern)
    for i in range(len(text) - p_len + 1):
        window = text[i:i+p_len]
        if hash(window) == p_hash and window == pattern:
            print(f"Match at index {i}")
This code slides over the text, compares hash of each window with pattern hash, then confirms by direct string comparison.
Execution Table
StepOperationCurrent WindowPattern HashWindow HashHash Match?Exact Match?ActionVisual State
1Compute pattern hash-hash('abc')=963----Pattern hash=963
2Compute initial window hashtext[0:3]='abc'963hash('abc')=963Yes--Window='abc' hash=963
3Compare exact stringsabc963963YesYesReport match at index 0Match at index 0
4Slide window by 1text[1:4]='bcd'963hash('bcd')=964No-No matchWindow='bcd' hash=964
5Slide window by 1text[2:5]='cde'963hash('cde')=965No-No matchWindow='cde' hash=965
6Slide window by 1text[3:6]='def'963hash('def')=966No-No matchWindow='def' hash=966
7End-963----No more windows
💡 Reached end of text windows; all possible matches checked.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5After Step 6Final
p_hash-963963963963963963
window--'abc''bcd''cde''def'-
window_hash--963964965966-
current_index--0123-
Key Moments - 3 Insights
Why do we compare the actual strings after hash match?
Because different strings can have the same hash (hash collision). The execution_table rows 3 and 4 show hash matches but exact string check confirms real match.
How does sliding the window update the hash efficiently?
In this simple example, hash is recomputed fully each time (rows 4-6). In optimized Rabin-Karp, rolling hash updates by removing left char and adding right char.
Why does the loop stop at len(text) - len(pattern) + 1?
Because windows beyond that length would be shorter than the pattern and cannot match. The exit at row 7 shows no more windows to check.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the window hash at step 4?
A963
B964
C965
D966
💡 Hint
Check the 'Window Hash' column at step 4 in the execution_table.
At which step does the algorithm report a match?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column for when 'Report match' happens.
If the pattern was 'bcd', at which step would the first match be reported?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Current Window' and 'Action' columns to see when 'bcd' window appears.
Concept Snapshot
Rabin Karp String Matching:
- Compute hash of pattern
- Slide window over text, compute window hash
- If hashes match, compare strings
- Report match if exact
- Efficient with rolling hash (not shown here)
- Stops when windows run out
Full Transcript
Rabin Karp string matching works by first computing a hash value for the pattern. Then it slides a window of the same length over the text, computing the hash for each window. If the window hash matches the pattern hash, it compares the actual strings to confirm a match. This avoids unnecessary string comparisons. The process repeats until all possible windows in the text are checked. The example code computes full hashes each time, but optimized versions use rolling hash to update hashes efficiently. The execution table shows each step, the current window, hashes, and actions taken. Matches are reported only after confirming exact string equality. The loop stops when no more windows of pattern length remain in the text.