Z Algorithm: A Powerful String Searching Technique šŸŽÆ

beginner
23 min

Z Algorithm: A Powerful String Searching Technique šŸŽÆ

Welcome to your new lesson on the Z Algorithm! This technique is a powerful tool in the world of string searching algorithms, and it's perfect for beginners and intermediates alike. Let's dive in!

What is the Z Algorithm? šŸ“

The Z Algorithm is a linear time string search algorithm that finds all occurrences of a pattern within a text. It's particularly useful when the pattern isn't known in advance, making it an excellent tool for applications like text editors, search engines, and data analysis.

Why Use the Z Algorithm? šŸ’”

  1. Efficiency: The Z Algorithm has a time complexity of O(n), making it significantly faster than other string searching algorithms like the naive string matching algorithm (O(n^2)) or the KMP algorithm (O(n)).

  2. Flexibility: It can find all occurrences of a pattern within a text, making it versatile for various applications.

  3. Simplicity: Despite its efficiency, the Z Algorithm is relatively simple to understand and implement.

How Does the Z Algorithm Work? šŸ’”

The Z Algorithm works by creating a table (Z-array) that represents the length of the maximum prefix of the pattern that is also a suffix of the pattern starting at each position. This table helps us efficiently find all occurrences of the pattern in the text.

Implementing the Z Algorithm šŸ“

Here's a step-by-step guide on how to implement the Z Algorithm:

  1. Initialize the Z-array: Set all elements of the Z-array to zero.

  2. Left and Right Pointers: Initially, set the left pointer (l) to 0 and the right pointer (r) to the length of the pattern (n).

  3. Calculate Z-values: While l < r, calculate the Z-value for the current position (l) using the formula:

Z[l] = max(Z[l - k], k, r - l) - l

Increment l by the calculated Z-value and update the Z-array accordingly.

  1. Update r: If Z[l] < r - l, set r to l + Z[l]. Otherwise, set r to the minimum of r and l + Z[l].

  2. Find Occurrences: Once the Z-array is calculated, finding occurrences of the pattern in the text is straightforward. Start from the first character of the text and slide a window of size n along the text. If the current character in the text matches the corresponding character in the pattern, check if the text substring matches the pattern. If it does, record the starting index of the match.

Code Examples šŸ’”

Here's a simple implementation of the Z Algorithm in Python:

python
def z_algorithm(pattern): n = len(pattern) z = [0] * n l, r = 0, n for i in range(1, n): while r > i: if pattern[i] == pattern[z[i - 1]] and z[i - 1] > 0: z[i] = z[i - 1] i += z[i - 1] elif pattern[i] == pattern[r - 1]: z[i] = r - i r -= 1 else: z[i] = 0 return z def find_occurrences(text, pattern, z): n = len(text) m = len(pattern) for i in range(n - m + 1): if all(text[i + j] == pattern[j] for j in range(m)): print(f"Pattern found at position {i}") pattern = "ATTACK" text = "DEFEND THE WEST GATE AGAINST THE VIKINGS ATTACK AT LAKE WINDER" z = z_algorithm(pattern) find_occurrences(text, pattern, z)

Quiz šŸ’”

Quick Quiz
Question 1 of 1

What is the time complexity of the Z Algorithm?

Happy coding, and remember: learning is a journey, and every step brings us closer to mastering the Z Algorithm! šŸš€šŸŒŸ