141. Rabin Karp Algorithm

Given a string text and a string pattern, implement the Rabin-Karp algorithm to find the starting index of all occurrences of pattern in text. If pattern is not found, return an empty list.

Example 1:

Input: text = "ababcabcababc", pattern = "abc"

Output: [2, 5, 10]

Expalanation : The pattern "abc" is found at indices 2, 5, and 10 in the text.

Example 2:

Input: text = "hello", pattern = "ll"

Output: [2]

Explanation: The pattern "ll" is found at index 2 in the text.

Now Your Turn!

Pick the correct output for the given input

Input: text = "abcdef", pattern = "gh"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1<= text.length, pattern.length <=5*104

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution{
public:
vector <int> search(string pat, string txt) {
}
};
Test Case

Input:

Pattern
Text