# #HackEatOfDay

Hey, Guys Learner is here with some facts about Z algorithm.

## What’s Z Algorithm?

Z algorithm finds all occurrences of a pattern in a text in linear time. Let the length of text be x and of the pattern be y, the total time taken is O(x+y) with linear space complexity. Now we can see that both time and space complexity is same as KMP algorithm but this algorithm is Simpler to understand.

#### Z Array

For a string str[0..n-1], Z array is of same length as string. An element Z[i] of Z array stores length of the longest substring starting from str[i] which is also a prefix of str[0..n-1]. The first entry of Z array is meaning less as complete string is always prefix of itself.

Example:Index 0 1 2 3 4 5 6 7 8 9 10 11 Text a a b c a a b x a a a z Z values X 1 0 0 3 1 0 0 2 2 1 0

More Examples:str = "aaaaaa" Z[] = {x, 5, 4, 3, 2, 1} str = "aabaacd" Z[] = {x, 1, 0, 2, 1, 0, 0} str = "abababab" Z[] = {x, 0, 6, 0, 4, 0, 2, 0}

## Construction of Z-Array

A Simple Solution is two run two nested loops, the outer loop goes to every index and the inner loop finds the length of the longest prefix that matches substring starting at current index. The time complexity of this solution is O(n2).

We can construct Z array in linear time.

The idea is to maintain an interval [L, R] which is the interval with max R such that [L,R] is prefix substring (substring which is also prefix). Steps for maintaining this interval are as follows – 1) If i > R then there is no prefix substring that starts before i and ends after i, so we reset L and R and compute new [L,R] by comparing str[0..] to str[i..] and get Z[i] (= R-L+1). 2) If i <= R then let K = i-L, now Z[i] >= min(Z[K], R-i+1) because str[i..] matches with str[K..] for atleast R-i+1 characters (they are in [L,R] interval which we know is a prefix substring). Now two sub cases arise – a) If Z[K] < R-i+1 then there is no prefix substring starting at str[i] (otherwise Z[K] would be larger) so Z[i] = Z[K] and interval [L,R] remains same. b) If Z[K] >= R-i+1 then it is possible to extend the [L,R] interval thus we will set L as i and start matching from str[R] onwards and get new R then we will update interval [L,R] and calculate Z[i] (=R-L+1).