Z Algorithm Visualizer

Enter text and a pattern, then step through the combined string, Z-box reuse, character comparisons, and match indexes.

Length
29
Matches
0
Z[i]
0
Z-box
none

String input

Delimiter: DELIM

Step controls

Build Z-array for pattern length 9 and text length 19.

Combined string

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
A
B
A
B
C
A
B
A
B
DELIM
A
B
A
B
D
A
B
A
C
D
A
B
A
B
C
A
B
A
B
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0
Z=0

Z table

IndexCharZStatus
0A0active
1B0pattern
2A0pattern
3B0pattern
4C0pattern
5A0pattern
6B0pattern
7A0pattern
8B0pattern
9DELIM0delimiter
10A0pending
11B0pending
12A0pending
13B0pending
14D0pending
15A0pending
16B0pending
17A0pending
18C0pending
19D0pending
20A0pending
21B0pending
22A0pending
23B0pending
24C0pending
25A0pending
26B0pending
27A0pending
28B0pending

Z output

index = 0
z_value = 0
z_box = none
matches = []
comparisons = 0
Pattern length = 9
Combined length = 29
No matches found yet.

What is a Z Algorithm Visualizer?

A Z Algorithm visualizer shows how the Z-array is built for a combined string like pattern + delimiter + text. Each Z value stores how many characters from that position match the prefix of the combined string.

This tool lets you enter text and a pattern, then step through the Z-box, reused values, direct comparisons, and confirmed pattern match indexes.

For the full algorithm set, browse the Data Structure Visualizers hub.

How to use this Z Algorithm visualizer

  • Enter the text to scan.
  • Enter the pattern to search for.
  • Click Run Z Algorithm to jump to the completed Z-array.
  • Step forward to inspect the current index, active Z-box, comparison characters, and match output.
  • Read any Z value greater than or equal to the pattern length after the delimiter as a match.

Z Algorithm vs KMP and Rabin-Karp

The Z Algorithm and KMP both use prefix information to avoid restarting a search from scratch. Z builds prefix-match lengths over one combined string, while KMP builds an LPS table for the pattern. Rabin-Karp uses rolling hashes instead of prefix lengths.

Compare this page with the KMP String Matching Visualizer, Rabin Karp Visualizer, and Edit Distance Visualizer.

Frequently Asked Questions

What does the Z value mean?
Z[i] is the length of the longest substring starting at index i that matches the prefix of the same string.
Why does pattern matching use a delimiter?
The delimiter separates the pattern from the text so prefix matches cannot accidentally cross from the pattern into the text prefix.
What is the Z-box?
The Z-box is the current interval [left, right] where the substring is known to match the prefix. Values inside the box can reuse earlier Z values before comparing new characters.
What is the time complexity of the Z Algorithm?
The Z Algorithm runs in O(n + m) time for pattern matching, where n is the text length and m is the pattern length.

Related tools