1
0
mirror of https://github.com/dholerobin/Lecture_Notes.git synced 2025-03-16 14:19:58 +00:00

Update Z-algorithm.md

This commit is contained in:
Aakash Panchal 2020-06-12 22:17:35 +05:30 committed by GitHub
parent da4058ef5b
commit 2975f082ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,6 @@
Z-function and z-algorithm
Z-algorithm is a **string-matching algorithm**, which is used to find a place where a string is found within a larger string. It uses the value of **z-function** for a given string.
Z-algorithm is a **string-matching algorithm**, which is used to find a place where a string is found within a larger string. For example, `p = "ab"` and `s = "abbbabab"`, then KMP will find us `[0,4,6]` because $s$ has 3 occurrences of `"ab"`. It uses the value of **z-function** to do so.
Let's first see what is a **z-function**.
@ -120,7 +120,7 @@ vector<int> z_function(string s) {
$O(N)$, as at each step of the algorithm $r$ at least increases one step, and the maximum possible value of r is $n-1$.
## Search for a string
## Z-algorithm
Z-algorithm is used to search all occurrences of pattern-string $p$ in a string $s$ in $O(N)$.
@ -167,6 +167,7 @@ int main()
}
```
Z-function can also be used to solve various string related problems. Let's see some applications.
## To find the period of a string