# Z Algorithm

AlgorithmsData StructurePattern Searching Algorithms

This algorithm is named Z Algorithm because, in this algorithm, we need to create a Z array. The size of the Z array is the same as the text size. This array is used to store the length of longest possible substring starting from the current character of the main string. At first, the pattern and the main text are concatenated with a special symbol which is not present in the text and pattern. If the P is pattern and T is the main text, then after concatenation, it would be P$T (Assuming$ is not present in the P and T).

For this algorithm, the time complexity is O(m+n) as m is the length of pattern and n is the length of the main string.

## Input and Output

Input:
Main String: “ABAAABCDBBABCDDEBCABC”, Pattern: “ABC”
Output:
Pattern found at position: 4
Pattern found at position: 10
Pattern found at position: 18

## Algorithm

fillZArray(conStr, ZArray)

Input − conStr is the concatenated string of pattern and the main text. ZArray to store indexes of the longest possible substring.

Output − The filled ZArray

Begin
n := length of conStr
windLeft := 0 and windRight := 0

for i := 1 to n, do
if i > windRight, then
windLeft := i and windRight := i
while windRight < n AND conStr[windRight-windLeft] =
conStr[windRight], do
increase windRight by 1
done
ZArray[i] := windRight – windLeft
decrease windRight by 1
else
k := i – windLeft
if ZArray[k] < windRight – i +1, then
ZArray[i] := ZArray[k]
else
windLeft := i
while windRight < n AND conStr[windRight-windLeft] =
conStr[windRight], do
increase windRight by 1
done
ZArray[i] := windRight – windLeft
decrease windRight by 1
done
End

zAlgorithm(text, pattern)

Input − The main text and the pattern to search

Output − Location where the pattern is found

Begin
conStr = concatenate pattern + “$” + text patLen := length of pattern len := conStr length fillZArray(conStr, ZArray) for i := 0 to len – 1, do if ZArray[i] = patLen, then print the location i – patLen – 1 done End ## Example #include<iostream> using namespace std; void fillZArray(string conStr, int zArr[]) { int n = conStr.size(); int windLeft, windRight, k; windLeft = windRight = 0; //initially window size is 0 for(int i = 1; i < n; i++) { if(i > windRight) { windLeft = windRight = i; //window size is 0 but position to i while(windRight < n && conStr[windRight-windLeft] == conStr[windRight]) { windRight++; //extend the right bound of window } zArr[i] = windRight-windLeft; windRight--; }else { k = i-windLeft; if(zArr[k] < windRight-i+1) zArr[i] = zArr[k]; //when kth item less than remaining interval else { windLeft = i; while(windRight < n && conStr[windRight - windLeft] == conStr[windRight]) { windRight++; } zArr[i] = windRight - windLeft; windRight--; } } } } void zAlgorithm(string mainString, string pattern, int array[], int *index) { string concatedStr = pattern + "$" + mainString;    //concat with special character
int patLen = pattern.size();
int len = concatedStr.size();
int zArr[len];
fillZArray(concatedStr, zArr);

for(int i = 0; i<len; i++) {
if(zArr[i] == patLen) {
(*index)++;
array[(*index)] = i - patLen -1;
}
}
}

int main() {
string mainString = "ABAAABCDBBABCDDEBCABC";
string pattern = "ABC";
int locArray[mainString.size()];
int index = -1;
zAlgorithm(mainString, pattern, locArray, &index);

for(int i = 0; i <= index; i++) {
cout << "Pattern found at position: " << locArray[i]<<endl;
}
}

## Output

Pattern found at position: 4
Pattern found at position: 10
Pattern found at position: 18