Bird
0
0
DSA Cprogramming~10 mins

Rabin Karp String Matching in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Rabin Karp String Matching
Start
Calculate pattern hash
Calculate initial text window hash
For each window in text
Compare pattern hash and window hash
Yes No
Check chars
Match?
Report
End
The algorithm calculates hash of the pattern and text windows, compares hashes, and if equal, checks characters to find matches.
Execution Sample
DSA C
pattern = "abc";
text = "abdabcbabc";
for each window in text:
  if hash(window) == hash(pattern):
    check characters
  slide window and update hash
This code slides a window over the text, compares hash values with the pattern hash, and confirms matches by character check.
Execution Table
StepOperationWindow Start IndexPattern HashWindow HashHash Match?Character CheckMatch FoundVisual State
1Calculate pattern hash-294-----
2Calculate initial window hash (text[0..2])0294294YesCompare 'abc' with 'abd'No"a b d" -> ...
3Slide window to text[1..3]1294303No-No"b d a" -> ...
4Slide window to text[2..4]2294294YesCompare 'abc' with 'dab'No"d a b" -> ...
5Slide window to text[3..5]3294294YesCompare 'abc' with 'abc'Yes"a b c" -> ...
6Slide window to text[4..6]4294303No-No"b c b" -> ...
7Slide window to text[5..7]5294294YesCompare 'abc' with 'cba'No"c b a" -> ...
8Slide window to text[6..8]6294294YesCompare 'abc' with 'bab'No"b a b" -> ...
9Slide window to text[7..9]7294294YesCompare 'abc' with 'abc'Yes"a b c" -> ...
10Slide window to text[8..10]-294-----
11End of text reached-294-----
💡 Reached end of text windows; no more windows to check.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10Final
pattern_hash-294294294294294294294294294294
window_start_index-01234567--
window_hash-294303294294303294294294--
match_found-NoNoNoYesNoNoNoYes--
Key Moments - 3 Insights
Why do we check characters even if the hashes match?
Because different strings can have the same hash (hash collision). The character check confirms the match, as shown in steps 2, 4, and 5 in the execution_table.
How is the window hash updated when sliding the window?
The hash is updated by removing the leftmost character's contribution and adding the new rightmost character's contribution, avoiding full recomputation. This is why window_hash changes between steps, e.g., step 3.
Why does the algorithm stop after the last window?
Because the window cannot slide beyond the text length minus pattern length. Step 11 shows the end of text windows reached.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the window start index and was a match found?
AStart index 3, match found Yes
BStart index 3, match found No
CStart index 2, match found Yes
DStart index 4, match found Yes
💡 Hint
Check the 'Window Start Index' and 'Match Found' columns in row for step 5.
At which step does the window hash first differ from the pattern hash?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Window Hash' and 'Pattern Hash' columns in the execution_table rows.
If the pattern was "abd" instead of "abc", how would the match found at step 2 change?
AMatch found would be Yes
BMatch found would be No
CWindow hash would not be calculated
DAlgorithm would stop early
💡 Hint
Consider that the pattern hash would change and compare with window hash at step 2.
Concept Snapshot
Rabin Karp String Matching:
- Compute hash of pattern and initial text window
- Slide window over text, update hash efficiently
- If hashes match, check characters to confirm
- Handles collisions by verification
- Stops when window reaches text end
Full Transcript
Rabin Karp string matching works by calculating a hash value for the pattern and for each window of text of the same length. It slides the window one character at a time, updating the hash efficiently without recomputing from scratch. When the hash of the current window matches the pattern hash, it checks the characters one by one to confirm a real match, because different strings can have the same hash. The process continues until the window reaches the end of the text. This method speeds up searching by avoiding unnecessary character comparisons when hashes differ.