156. Z function

Given two strings, one is a text string, text, and the other is a pattern string, pattern. Print the indexes of all occurrences of the pattern string in the text string using the Z function algorithm. Use 0-based indexing while returning the indices.

Example 1:

Input: text = "xyzabxyzabxyz", pattern = "xyz"

Output: 0 5 10

Expalanation : The pattern "xyz" occurs three times in text, starting at indices 0, 5, and 10.

Example 2:

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

Output: 1

Expalanation : The pattern "abc" occurs one time in text, starting at index 1.

Now Your Turn!

Pick the correct output for the given input

Input: text = "aaabaaaabaaa", pattern = "aa"

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 text , string pattern){
 
}
};
 
 
Test Case

Input:

Pattern
Text