06. Search Pattern (Rabin-Karp Algorithm)

βœ… GFG solution for Rabin-Karp based substring search. Fast, efficient pattern matching using rolling hash! πŸš€

The problem can be found at the following link: πŸ”— Question Link

🧩 Problem Description

Given two strings:

  • A text string in which to search.

  • A pattern string to find.

Return all starting positions (1-based) where the pattern occurs in the text. Use the Rabin-Karp Algorithm (rolling hash-based pattern matching).

πŸ“˜ Examples

Example 1

Input: text = "birthdayboy", pattern = "birth"
Output: [1]

Example 2

Input: text = "geeksforgeeks", pattern = "geek"
Output: [1, 9]

πŸ”’ Constraints

  • $1 \leq \text{len(text)} \leq 5 \times 10^5$

  • $1 \leq \text{len(pattern)} \leq \text{len(text)}$

  • All characters are lowercase English letters (a-z)

βœ… My Approach

The Rabin-Karp Algorithm uses hashing to match the pattern with substrings in the text.

Hashing Basics

  • Convert strings to numerical hash using a rolling hash formula.

  • If two hashes match, perform a direct string comparison to confirm (to avoid false positives from collisions).

Algorithm Steps:

  1. Hash Function: Use a prime modulus q and radix/base d = 256 (covers lowercase letters).

  2. Precompute Hashes:

    • Compute the hash of the pattern and the initial window in text.

    • Use a rolling hash to update hash as the window slides by 1 character.

  3. Match Check:

    • When hash matches, check characters one by one to confirm.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(N + M), where N is text length and M is pattern length. We compute hashes in linear time and only compare strings when hashes match.

  • Expected Auxiliary Space Complexity: O(1), as we only use a fixed number of variables for hashing.

πŸ§‘β€πŸ’» Code (C++)

⚑ View Alternative Approaches with Code and Analysis

πŸ“Š 2️⃣ Naive Matching (Brute Force)

πŸ’‘ Idea:

Try all substrings of text of length m and compare with the pattern.

πŸ“ Complexity:

  • Time: O(N Γ— M)

  • Space: O(1)

βœ… Pros:

  • Easy to understand and implement

⚠️ Cons:

  • Slow for large inputs

⚠️ Warning: TLE on Large Inputs

βœ… Test Cases Passed: 1111 / 1115

❌ Result: Time Limit Exceeded (TLE)

πŸ†š Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

πŸ” Rabin-Karp

🟒 O(N + M)

🟒 O(1)

Fast and scalable

Hash collisions (rare)

🐒 Naive Substring Match (TLE)

πŸ”Έ O(N Γ— M)

🟒 O(1)

Very simple

Slow for large strings

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ“ˆ Large strings and fast matching required

πŸ₯‡ Rabin-Karp Algorithm

πŸ“‹ Simple brute-force acceptable

πŸ₯ˆ Naive Substring Match

πŸ§‘β€πŸ’» Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated