String Algorithms

Z-Algorithm

The Z-algorithm produces the ”-array” in time, where each element of the array indicates the length of the longest substring of that begins at position and is a prefix of .

Resources

For example, here are the values of the Z-function computed for different strings:

  • “aaaaa” - 
  • “aaabaab” - 
  • “abacaba” -

Why is it 0 for the first index?

Z[0] is 0 here by convention. From cp-algorithms website:

  • “The first element of Z-function,    , is generally not well defined. In this article we will assume it is zero (although it doesn’t change anything in the algorithm implementation).”

Many string processing problems can be efficiently solved using the Z-array.

vector z_function(string s) { 
	int n = s.size(); 
	vector z(n); 
	int x = 0, 
	y = 0; 
	for (int i = 1; i < n; i++) { 
		z[i] = max(0,min(z[i-x],y-i+1)); 
		while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { 
			x = i; 
			y = i+z[i]; 
			z[i]++; 
		} 
	} 
	return z; 
}

Problems that you can solve with the Z-Algorithm

Search the substring: find all occurrences of the pattern  inside the text . Solution: Construct a new string , we use as a separator. Then, go over the z-array and count the number of indices where