Both Z and the function prefix can be used to implement the KMP(Knuth-Morris-Pratt) algorithm for finding a substring in a string in O(|S|). The essence of this algorithm is as follows: we attribute to the string we want to find the string in which we are searching. It is highly desirable to put a separator character between these lines, i.e. a character that does not occur in any line (usually #).