Skip to main content

z_array

Function z_array 

Source
pub fn z_array(s: &[u8]) -> SeqResult<Vec<usize>>
Expand description

Compute the Z-array of s in O(n) time using the Z-box method.

The returned vector has length s.len(). Index 0 holds 0 by the convention documented at the module level; for 1 ≤ i < n, entry i is the length of the longest common prefix of s and the suffix s[i..].

§Errors

Returns SeqError::EmptyInput when s is empty: the Z-array of the empty string is itself empty, and rejecting it makes the empty/degenerate contract explicit and consistent with the rest of this module.

§Examples

use oxicuda_seq::string::z_array;

let z = z_array(b"aabxaabxcaabxaabxay").expect("non-empty");
assert_eq!(z[0], 0); // by convention
assert_eq!(z[4], 4); // "aabx" repeats at offset 4