Abstract 1 Introduction 2 Model and Definitions 3 Set Parameterized Comparison 4 Set Parameterized Matching Algorithm 5 Analysis References Appendix A Proof of Correctness for the Offset Sets Appendix B Proof of Theorem 2 Appendix C Proof of Complexity Analysis Appendix D Algorithms

Set Parameterized Matching via Multi-Layer Hashing

Moshe Lewenstein ORCID Bar-Ilan University, Ramat Gan, Israel    Ely Porat ORCID Bar-Ilan University, Ramat Gan, Israel
Abstract

We study the set parameterized matching problem, a generalization of the classical parameterized matching problem introduced by Baker [7, 8]. In set parameterized matching, both the pattern and text are sequences where each position contains a set of characters rather than a single character. Two set-strings parameterized match if there exists a bijection between their alphabets that maps one to the other set-wise. Boussidan [12] introduced this problem for the case of equal-length set-strings. We present a randomized algorithm running in O(N+M) time with high probability, where N is the text size and M is the pattern size. Our approach employs a novel three-layer hashing scheme based on Karp-Rabin fingerprinting that addresses the challenges of (1) the size blowup in representations of the problem, (2) set-to-set matching, and (3) the dynamic nature of encodings of text substrings during pattern scanning.

Keywords and phrases:
Set Parameterized Matching, Pattern Matching, Randomized Algorithms, Hashing, Parameterized Matching
Funding:
Moshe Lewenstein: Work on this paper was supported by ISF grant # 2760/25 awarded by the Israel Science Foundation.
Copyright and License:
[Uncaptioned image] © Moshe Lewenstein and Ely Porat; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Pattern matching
; Mathematics of computing Probabilistic algorithms
Editors:
Philip Bille and Nicola Prezza

1 Introduction

The parameterized pattern matching problem, introduced by Baker [6, 7, 8], seeks all occurrences of a pattern P[1..m] in a text T[1..n] where an occurrence is defined not by exact character matches but by consistent character renaming. Formally, the pattern occurs at position i if there exists a bijection π from the pattern alphabet to the text alphabet such that T[i+j1]=π(P[j]) for all j[1..m]. This problem has found numerous applications in software maintenance [6], plagiarism detection, computational biology [25], and beyond [24].

1.1 Prior Work

The study of parameterized matching was pioneered by Baker [6, 7], who introduced the concept to identify duplicated code in large software systems. The core innovation was the “predecessor string” (or prev) representation, which converts a string into an integer sequence where each character is replaced by the offset to its previous occurrence. A key observation is that two strings parameterized match if and only if their prev transformations are identical. This insight allowed for efficient matching regardless of specific variable names, provided the usage patterns were consistent. This discovery enabled O(nlogmin(m,|ΣP|))-time algorithms [1] and led to the development of parameterized suffix trees [8] and suffix arrays [14]. Subsequent research has explored the theoretical boundaries and practical variations of the problem:

  • Approximate matching: Significant work has been done on matching with k mismatches [4, 18], edit distance [9, 5], lcs [20] and δγ-distances [22].

  • Compressed matching: Solutions have been developed for searching in compressed text [17], run-length encoded strings [3], and other compression schemes [10, 21].

  • Higher dimensions and Structures: The problem has been extended beyond linear strings to matching on trees [2] and two-dimensional grids [13].

  • Succinct data structures: Recent developments include space-efficient dictionaries [15, 16] and the parameterized Burrows-Wheeler Transform (pBWT) [16].

For a comprehensive overview of the field’s evolution, Lewenstein [23] and Mendivelso et al. [24] provide a detailed historical account of these problems and their various applications.

1.2 Set Parameterized Matching

While classical parameterized matching handles sequences of single characters, many real-world scenarios involve ambiguity or multiple valid interpretations at a single position. Boussidan [12] in his PhD thesis (see also [11]) was concerned about comparing 18th century French plays, where the characters of the play are important, but each scene has multiple characters within. To address this, Boussidan extended the model of parameterized matching to set parameterized comparison, where for two equal-length strings, each position in both contains a set of characters, and the question is whether they parameterize match. We call these strings containing sets, set-strings. Boussidan’s original algorithm relied on bipartite matching. This approach is computationally expensive, requiring O(m|Σ|2.5) time for comparison alone. In this paper, we consider the full pattern matching version of the problem:

Problem 1 (Set Parameterized Matching).

Given a text 𝒯=𝒯[1..n] and a pattern 𝒫=𝒫[1..m] where each 𝒯[i],𝒫[j]Σ, report all positions i[1..nm+1] such that 𝒫 set-parameterized matches 𝒯[i..i+m1].

1.3 Our Contributions

We present a linear time randomized algorithm for set parameterized matching which is novel in its solution approach. This even dramatically improves the previous bipartite matching approaches for the special case of set parameterized comparison.

Theorem 1 (Main Result).

Given a set-string text 𝒯[1..n] and set-string pattern 𝒫[1..m] over alphabet Σ, where N=i=1n|𝒯[i]| and M=j=1m|𝒫[j]|, we can report all set-parameterized matches in time O(N+M) with high probability using a Monte Carlo randomized algorithm with error probability at most 1/n.

Key to our result is the introduction of a new structural definition we term the multiset offset representation. Unlike classical predecessor pointers which capture only local history, this representation preserves the global distribution of every character, ensuring that the necessary bijection properties hold even when multiple characters share identical positions. To process this representation, which can be quadratically large in the worst case, we employ a novel three-layer hashing scheme:

  1. 1.

    Layer 1: Hash the offset set of each character to a single integer using Karp-Rabin fingerprinting, compressing the interaction history.

  2. 2.

    Layer 2: Hash the multiset of these values at each position to a single integer (“mashing”), enabling efficient set-to-set comparisons that respect multiplicities.

  3. 3.

    Layer 3: Apply a rolling Karp-Rabin hash to the sequence of mashed values to enable linear time processing of sliding windows.

As a warmup to the main result, we also demonstrate an efficient solution for the comparison problem:

Theorem 2 (Set-String Comparison).

Given two set-strings 𝒮1[1..m] and 𝒮2[1..m] over alphabet Σ, where M𝒮1=i=1m|𝒮1[i]| and M𝒮2=i=1m|𝒮2[i]|, we can determine whether they set-parameterized match in time O(M𝒮1+M𝒮2) with error probability at most 1/m.

1.4 Organization

The remainder of the paper is organized as follows. Section 2 defines our model and the necessary notation. Section 3 serves as a warmup, proving Theorem 2 for comparing two set-strings. Section 4 details our main pattern matching algorithm, proving Theorem 1. Finally, we analyze the correctness and complexity in Section 5.

2 Model and Definitions

2.1 Notation and Basic Definitions

Throughout this paper, we denote by Σ a finite alphabet and by Σ the set of all finite strings over Σ. We assume that Σ={1,2,,|Σ|}. If not, we sort Σ and rename the alphabet characters. For a string SΣ, we write |S| for its size (length) and S[i] for its i-th character, where i[1..|S|]. We use the notation [n]:={1,2,,n} for positive integers n. For a prime p, we denote the field p:={0,1,,p1}, the integers modulo p.

2.2 Classical Parameterized Matching

We begin by recalling the classical parameterized matching framework of Baker [7, 8], which forms the foundation of our work.

Definition 3 (Parameterized Match [7]).

Let P[1..m],T[1..m]Σ be two strings of equal length over alphabet Σ. We say that P and T parameterized match, denoted PpT, if there exists a bijection π:ΣΣ such that T[i]=π(P[i]) for all i[1..m].

 Remark 4.

The bijection π in Definition 3 must be defined on the entire alphabet Σ, though it need only be consistent on the characters actually appearing in P and T. Some works distinguish between “constant” and “parameter” alphabets ΣCΣP where π acts as the identity on ΣC; for simplicity, we consider the case where all characters are parameters (equivalently, ΣC=).

Problem 2 (Parameterized Pattern Matching).
Input:

A text T[1..n] and a pattern P[1..m] over alphabet Σ.

Output:

All positions i[1..nm+1] such that PpT[i..i+m1].

Baker’s key insight was to transform the string into a structural representation invariant under permutation. This is the prev transformation (or predecessor encoding):

Definition 5 (Prev Transformation [7]).

For a string S[1..n] over alphabet Σ, the prev transformation prev(S) is the integer sequence prev(S)[1..n] where

prev(S)[i]={0if S[i] first occurs at position iijotherwise, where j=max{k<i:S[k]=S[i]}
Lemma 6 (Correctness of Prev [7]).

For strings P[1..m],T[1..m] over Σ, we have PpT if and only if prev(P)=prev(T) (equality of integer sequences).

2.3 Set-Strings and Set Parameterized Matching

We now formalize the set parameterized matching model, which generalizes the concepts above to sequences of sets.

Definition 7 (Set-String).

A set-string 𝒮 of length n over alphabet Σ is a sequence 𝒮[1..n] where each 𝒮[i]Σ is a finite set of characters. The length of 𝒮 is n and its size is |𝒮|=j=1n|𝒮[j]|. We write 𝒮[i..j] for the subsequence 𝒮[i],𝒮[i+1],,𝒮[j].

Definition 8 (Set Parameterized Match).

Let 𝒮1[1..m] and 𝒮2[1..m] be two set-strings of equal length over alphabet Σ. We say that 𝒮1 and 𝒮2 set-parameterized match, denoted 𝒮1p𝒮2, if there exists a bijection π:ΣΣ such that for all i[1..m]: π(𝒮1[i])=𝒮2[i] where π(A):={π(a):aA} for any set AΣ.

 Remark 9.

Definition 8 requires that the bijection π simultaneously rename all characters in all sets consistently. This generalizes Definition 3: if each set 𝒫[i] and 𝒯[i] is a singleton, we recover classical parameterized matching.

Problem 3 (Reformulation of Set Parameterized Matching).
Input:

A text 𝒯[1..n] and a pattern 𝒫[1..m], both set-strings over alphabet Σ.

Output:

All positions i[1..nm+1] such that 𝒫p𝒯[i..i+m1].

For the special case where n=m we call the problem the Set Parameterized Comparison as all that is required is to verify if the strings parameterize match each other.

2.4 Offset Representation

A naive extension of the prev transformation to set-strings – where one replaces every character in every set with its predecessor offset – fails because it destroys the continuity of character chains. Consider the following counter-example: Let set-string 𝒮1={,{b},,,{a},,,{a,b},,,,,{a}} and 𝒮2={,{c},,,{d},,,{c,d},,,,,{c}} If we simply replace each character with its prev value, the resulting sets of prevs match perfectly at every position: prev(𝒮1)=prev(𝒮2)=

{,{0},,,{0},,,{3,6},,,,,{5}}

The sets of prevs are identical, yet there is no valid parameterization. In 𝒮1, the character appearing at 13 (“a”) is the one that started at 5. In 𝒮2, the character at 13 (“c”) is the one that started at 2. The simple set of prevs {3,6} at position 8 obscures which prev belongs to which character chain. To overcome this, we represent all appearances of a specific character “together” as a structural unit. While a “prev set” (listing all predecessor offsets for a character) might capture this structure, it is not convenient for the rolling hash updates required by our algorithm. Instead, we chose to represent this “togetherness” using offset sets. Since a character may appear in multiple sets, and a set contains multiple characters, a single “previous occurrence” is ill-defined. We must instead track the set of previous offsets. This motivates the following definition.

Definition 10 (Offset Set).

Let 𝒮[1..n] be a set-string over Σ, and let σΣ. Suppose σ appears in 𝒮 at positions i1<i2<<ik (i.e., σ𝒮[ij] for all j[1..k]). The offset set of σ in 𝒮 is

OffSet𝒮(σ):={iji1:j[1..k]}{0,1,2,,n1}.

If σ does not appear in 𝒮, we define OffSet𝒮(σ):=.

Note that multisets are necessary as we may have two characters in one set with the same offset set. We need to keep both of them as the multiplicity of characters in the compared set-strings must be equal. With the offset set defined for each individual character, we now aggregate this information to represent the structure of the entire set-string. At any given position i in the string, multiple characters may be present. To fully capture the parameterization constraints at this position, we must collect the offset sets of all characters appearing there, forming the signature of that position, allowing us to compare the structural roles of characters across two different strings.

Definition 11 (Multiset Offset Representation).

For a set-string 𝒮 of length m, the offset representation at position i, denoted 𝒮^[i], is the multiset of offset sets for all characters appearing at 𝒮[i].

𝒮^[i]={OffSet𝒮(σ)σ𝒮[i]}

and

𝒮^=𝒮^[1]𝒮^[2]𝒮^[n]

In continuation with the former example:

𝒮1^ ={,{{0,6}},,,{{0,3,8}},,,{{0,6},{0,3,8}},,,,,{{0,3,8}}} and
𝒮2^ ={,{{0,6,11}},,,{{0,3}},,,{{0,6,11},{0,3}},,,,,{{0,6,11}}}
Observation 12.

For a set-string 𝒮[1..n] over alphabet Σ, each offset set OffSet𝒮(σ) has size at most n. Therefore, 𝒮^[i] contains exactly |𝒮[i]| offset sets, each of size at most n. Letting N=i=1n|𝒮[i]| denote the total number of character occurrences, in the worst case, |𝒮^| (the total number of integers in the offset representation) is O(nN).

The following lemma, proof in appendix, shows that the Multiset Offset Representation provides a necessary and sufficient condition for set parameterized matching.

Lemma 13.

Let 𝒮1 and 𝒮2 be set-strings of length m. Then 𝒮1p𝒮2 if and only if 𝒮1^=𝒮2^ (where equality implies equality of the sequence of multisets).

2.5 Challenges for Set Parameterized Comparison and Matching

While Lemma 13 successfully reduces set-parameterized comparison to checking equality of offset representations, it does not immediately yield an efficient algorithm for set parameterized comparison and, definitely, not for set parameterized matching, due to two distinct hurdles:

  1. 1.

    Size blowup: By Observation 12, the offset representation can be quadratically larger than the input. Comparing these representations directly is prohibitively expensive.

  2. 2.

    Dynamic offset sets: For set parameterized matching, we compare 𝒫 against sliding windows 𝒯[i..i+m1]. The offset sets OffSet𝒯[i..i+m1](σ) depend on the start index i and must be recomputed for each position. This contrasts with classical pattern matching, where the text “characters” remain static throughout the algorithm.

Our algorithms addresses these challenges via a multi-layer hashing scheme, detailed in Sections 3 and 4.

2.6 Karp-Rabin Fingerprinting

Our algorithms employ the Karp-Rabin fingerprinting technique [19]. We distinguish between two variants of the hash function, depending on whether the input is treated as a sequence (order matters) or a multiset (order irrelevant).

Definition 14 (Sequence Hash).

Let p be a prime and rp. For a sequence of integers A=(a0,a1,,ak1), the sequence hash treats the elements as coefficients of a polynomial:

hp,rseq(A):=i=0k1airimodp.
Definition 15 (Set/Multiset Hash).

Let S be a multiset of integers drawn from a universe {0,,U1}. Let cv denote the multiplicity of value v in S. The set/multiset hash treats the values as exponents:

hp,rset(S):=v=0U1cvrvmodp.
Lemma 16 (Collision Probability [19]).

Let p be a prime and rp be chosen uniformly at random.

  1. 1.

    Sequence Collision: Let A and B be two distinct sequences of length at most . Then:

    Pr[hp,rseq(A)=hp,rseq(B)]1p
  2. 2.

    Multiset Collision: Let S1 and S2 be two distinct multisets containing values from the universe {0,,U1}. Then:

    Pr[hp,rset(S1)=hp,rset(S2)]Up
Proof.

In both cases, the collision condition h(X)=h(Y) is equivalent to r being a root of the non-zero difference polynomial Q(x)=h(X)h(Y) over the field p. By the fundamental theorem of algebra, the number of roots is bounded by the degree of Q(x).

  1. 1.

    For sequences, Q(r)=(aibi)ri. The degree is bounded by the maximum index, which is 1.

  2. 2.

    For multisets, Q(r)=(c1,vc2,v)rv. The degree is bounded by the maximum value v such that counts differ, which is at most U.

Dividing the degree by the field size p yields the probability bound.

2.7 Rolling Hashes

In the classical Rabin-Karp pattern matching algorithm [19], the rolling hash essentially computes a fingerprint for every window of length m in the text, and compares it to the fingerprint of the pattern.

Definition 17 (Rolling Hash).

For a sequence S[1..n] where each S[i]p, and a window size mn, define

Hp,r(m)(S,i):=j=0m1S[i+j]rjmodp

for i[1..nm+1]. This is the rolling hash of the window S[i..i+m1].

A key property of this scheme is the ability to efficiently update the hash value as the window slides from one position to the next.

Observation 18 (Efficient Rolling Hash Update).

Given Hp,r(m)(S,i), we can compute Hp,r(m)(S,i+1) in O(1) time via

Hp,r(m)(S,i+1)=Hp,r(m)(S,i)S[i]r+S[i+m]rm1modp.

Our algorithm, to appear later, builds upon this rolling concept. However, adapting it to parameterized matching, and specifically set parameterized matching, introduces significant complexity: the effective value of a “character” (its offset) is relative to the window’s start position and thus changes dynamically as the window rolls.

3 Set Parameterized Comparison

In this section, we prove Theorem 2 by presenting an efficient algorithm for set parameterized comparison, i.e., determining whether two set-strings of equal length set-parameterized match. This algorithm serves as a conceptual warmup for our main pattern matching algorithm and introduces the first two layers of our hashing scheme.

3.1 Algorithm Overview

The algorithm proceeds in three logical steps to progressively compress the set-string structure:

  1. 1.

    First, we compute the explicit offset sets for all characters in both set-strings.

  2. 2.

    Next, we hash each offset set to a single integer fingerprint using Karp-Rabin hashing (Layer 1).

  3. 3.

    Finally, we hash the set of fingerprints at each position to obtain a single value, allowing for linear comparisons (Layer 2).

3.2 Layer 1: Hashing Offset Sets

We first address the computational cost of generating the offset sets. Recall that Σ={1,2,,|Σ|}.

Lemma 19 (Offset Set Computation).

Given a set-string 𝒮[1..m] over alphabet Σ of size M=i=1m|𝒮[i]|, we can compute OffSet𝒮(σ) for all σΣ in time O(M) and space O(M).

Proof.

We perform a single pass over the set-string. We maintain a hash table FirstOcc:Σ[1..m]{} to track the first occurrence of each character, and for each σΣ, a list Offsets[σ] initially empty.

The initialization loop takes O(|Σ|) time, which is O(M). The outer loop runs m times, and the inner loop processes each character in each set. In total, we process exactly M=i=1m|𝒮[i]| character-position pairs. Each operation takes O(1) expected time with hashing, giving a total time complexity of O(M).

Once the offset sets are computed, we compress them using the set hash function from Def. 15.

Definition 20 (Layer 1 Hash).

Let p1 be a prime and let r1p1 be chosen uniformly at random. For an offset set O{0,1,,m1}, the Layer 1 hash function is:

ϕ1(O):=hp1,r1set(O)=xOr1xmodp1.
Collision Analysis.

By the definition, the Layer 1 hash is exactly the multiset hash ϕ1(O)=hp1,r1set(O). The elements of the offset sets are integers drawn from the universe {0,,m1}. We apply Lemma 16 (Multiset Collision case) with universe bound U=m, yielding for O1O2:

Pr[ϕ1(O1)=ϕ1(O2)]mp1.

3.3 Layer 2: Hashing Sets of Fingerprints

After applying Layer 1, each position i in set-string 𝒮 is represented by a multiset of fingerprints:

Φ1(𝒮[i]):={ϕ1(OffSet𝒮(σ)):σ𝒮[i]}.

To compare positions efficiently, we must compare these multisets. We therefore apply a second layer of hashing to “mash” these multisets into single values. Specifically, we use the multiset hash function from Definition 15.

Definition 21 (Layer 2 Hash).

Let p2 be a prime and let r2p2 be chosen uniformly at random. For a multiset F of fingerprints from p1, the Layer 2 hash function is:

ϕ2(F):=hp2,r2set(F)=fFr2fmodp2.

We call the resulting hash value the mashed value of the position and the mashed representation of a set-string 𝒮[1..m] is the sequence (𝒮)=(M1,M2,,Mm) where Mi:=ϕ2(Φ1(𝒮[i])).

Collision Analysis.

By the definition, Layer 2 is also the multiset hash ϕ2(F)=hp2,r2set(F). The elements of the multisets are Layer 1 fingerprints, which are integers drawn from the universe {0,,p11}. Therefore, we can apply Lemma 16 (Multiset Collision case) with universe bound U=p1, yielding:

Prr2[ϕ2(F1)=ϕ2(F2)]p1p2

3.4 Set Parameterized Comparison Algorithm

The full procedure for set parameterized comparison, comparing two set-strings, consisting of applying the 2-level hash to each, is formalized in Algorithm 1 and appears in the appendix.

Theorem 22 (Correctness of Comparison Algorithm).

Algorithm 1 correctly determines whether 𝒮1p𝒮2 with probability at least 1m1.

Proof.
Completeness.

Assume 𝒮1p𝒮2. By Lemma 13, we know 𝒮1^=𝒮2^. This implies that for every position i and every σ𝒮1[i], there exists a corresponding τ𝒮2[i] such that OffSet𝒮1(σ)=OffSet𝒮2(τ), and vice versa. It follows deterministically that:

  • Φ1(𝒮1[i])=Φ1(𝒮2[i]) for all i (as sets), and consequently,

  • Mi𝒮1=Mi𝒮2 for all i.

Thus, the algorithm correctly returns true.

Soundness.

Assume 𝒮1p𝒮2. By Lemma 13, 𝒮1^𝒮2^. Thus, there must exist some position i where the sets of offset sets differ: 𝒮1^[i]𝒮2^[i]. Let 1 be the event that “there exist distinct offset sets O1,O2 with ϕ1(O1)=ϕ1(O2).” By choosing p1=Θ(m2|Σ|2) and taking the union bound over all (|Σ|2)|Σ|2 pairs of distinct characters, we bound the probability of this collision:

Pr[1]|Σ|2mp1=O(m|Σ|2m2|Σ|2)=O(1m).

Conditioned on ¬1, the hash function ϕ1 is injective on the offset sets appearing in 𝒮1 and 𝒮2. Since 𝒮1^[i]𝒮2^[i], it must be that Φ1(𝒮1[i])Φ1(𝒮2[i]) as sets of fingerprints. Next, let 2 be the event that “there exist distinct sets F1,F2p1 with ϕ2(F1)=ϕ2(F2).” Choose p2=Θ(m4|Σ|2). We are comparing m multisets (one for each position i[1..m]). For any specific position, the collision probability is bounded by p1/p2 (by Lemma 16 with universe p1). Applying the union bound over all m positions:

Pr[2¬1]mp1p2=O(m3|Σ|2m4|Σ|2)=O(1m).

Conditioned on ¬1¬2, we are guaranteed that Mi𝒮1Mi𝒮2, and so the algorithm returns false. The overall error probability is bounded by the sum of probabilities of these collision events:

Pr[12]Pr[1]+Pr[2¬1]=O(1m).

This concludes the proof of Theorem 2, with a summary appearing in the index.

4 Set Parameterized Matching Algorithm

We now present our main algorithm for Problem 3, the problem of Set Parameterized Matching, proving Theorem 1.

4.1 The Rolling Hash Challenge

Recall from Section 2 (Definition 17) that the Rabin-Karp rolling hash allows for efficient O(1) updates when sliding a window over a fixed sequence of integers. In our context, the natural candidate for such a sequence is the sequence of Layer 2 mashed values, M1𝒯,M2𝒯,,Mn𝒯. A direct application of the standard rolling hash for a window of size m starting at text position i would be:

H=j=0m1Mi+j𝒯r3jmodp3

where p3 is some prime, r3[p3], and Mk𝒯 represents the Layer 2 hash of the offset sets at text position k.

The Dynamic Offset Problem

However, this standard approach fails because the values Mk𝒯 are not static. In set parameterized matching, the offset set OffSet𝒯[i..i+m1](σ) is defined relative to the first occurrence of σ inside the current window. When the window slides from i to i+1, if a character σ is dropped from position i, the “first occurrence” of σ changes to its next appearance in the window (say, at relative distance δ). Consequently, the offsets of all remaining occurrences of σ in the window must be decreased by δ. This changes the Layer 1 hashes (ψ) and, crucially, changes the Layer 2 mashed values (M𝒯) for every position where σ appears. Updating the rolling hash H would thus require recomputing up to O(m) terms in the summation, which is prohibitively slow.

4.2 Character-Centric Rolling Hash

To handle these dynamic updates efficiently, we restructure the hashing scheme. Instead of viewing H as a sum over window positions (which is sensitive to shifting offsets), we swap the order of summation to view it as a sum over alphabet characters. Recall that for the window starting at location i, Mi+j𝒯=σ𝒯[i+j]r2ψ(i)[σ]. Substituting this into the standard rolling hash definition:

H=j=0m1r3j(σ𝒯[i+j]r2ψ(i)[σ]modp2)modp3

By swapping the summation order, we group terms by character rather than by position:

H=σΣ(r2ψ(i)[σ]modp2)(j:σ𝒯[i+j]r3j)modp3

This rearrangement separates the structural shape of the encoding of σ (encoded by r2ψ(i)[σ]) from the geometric positions of the encoding of σ (encoded by the inner sum over r3). This separation allows us to update the two components independently.

This motivates our formal definition of the components for the character-centric hash. We first isolate the “geometric” component – the inner sum representing the positions of a character.

Definition 23 (Character Position Fingerprint).

Let p2 be a prime and let r2p2 be chosen uniformly at random. For a character σ in the window starting at text position i, let its position fingerprint be the sum of the position powers for all its occurrences:

Pi(σ):=kOccurrences of σ in windowr3kmodp3

where k{0,,m1} is the relative index of the occurrence within the window.

With the geometric component isolated, the total window hash can be expressed as a weighted sum of these fingerprints, where the weights are determined by the structural shape (offset sets) of each character.

Definition 24 (Layer 3 Hash).

The character-centric Layer 3 hash for the window at text position i is:

Hi𝒯:=σΣ(r2ψ(i)[σ]modp2)Pi(σ)modp3

where ψ(i)[σ] is the Layer 1 hash of the offset set of σ in window i.

4.3 Efficient Transition Logic

To achieve our time complexity goals, we must compute Hi+1𝒯 from Hi𝒯 in time proportional only to the number of characters entering or leaving the window, independent of the alphabet size |Σ|. When sliding the window from i to i+1, two distinct types of updates occur:

  1. 1.

    Geometric Shift: Every character remaining in the window shifts left by one position. This implies that for every σΣ, the position fingerprint changes: Pi+1(σ)=Pi(σ)r31.

  2. 2.

    Structural Change: For characters in 𝒯[i] (leaving) and 𝒯[i+m] (entering), the offset structure ψ(i)[σ] may change to a new value ψ(i+1)[σ].

A naive update is impossible because we cannot iterate over all σΣ to apply the geometric shift r31, nor can we simply multiply the total hash Hi𝒯 by r31 (since the structural weights r2ψ[σ] for active characters would be incorrectly shifted). Instead, we decompose the total hash into a Stable Component (characters with unchanged structure) and an Active Component (characters appearing at the window boundaries). We process the transition in three steps: Isolate, Shift, and Reintegrate. Let Sactive=𝒯[i]𝒯[i+m] be the set of characters undergoing structural updates.

Step 1: Isolate (Remove Active Terms)

First, we remove the contributions of all “active” characters from the current hash to isolate the “stable” portion. For any stable character σSactive, the structural hash ψ remains invariant (ψ(i)[σ]=ψ(i+1)[σ]).

Hstable=Hi𝒯σSactive(r2ψ(i)[σ]modp2)Pi(σ)modp3

This step requires O(|Sactive|) operations.

Step 2: Global Shift (Update Stable Terms)

The remaining hash Hstable consists essentially of a sum of terms CσPi(σ) where Cσ is constant. The only required update is the geometric shift of positions (kk1). By the linearity of the sum, we can apply this shift globally:

Hstable=Hstabler31modp3

This single multiplication updates the hash for all non-active characters in O(1) time. Lazy Fingerprint Maintenance: To support this global shift without updating individual P(σ) values in memory, we maintain a global rolling multiplier Rglob. The stored value for a fingerprint is related to the actual value by Pactual(σ)=Pstored(σ)Rglob. In this step, we simply update RglobRglobr31.

Step 3: Reintegrate (Add Updated Active Terms)

Finally, we compute the new states for the active characters and add them back to the shifted stable hash. For each σSactive:

  • Update Position: We update Pstored(σ) to reflect any removals (at relative index 0) or insertions (at relative index m1). Note that we must multiply any added terms by Rglob1 to align with the current global frame.

  • Update Offset Structure:

    • Leaving (σ𝒯[i]): We must remove the first occurrence. Let δ be the distance to the next occurrence of σ in the window. The structure updates by shifting the exponents:

      ψ(i+1)[σ](ψ(i)[σ]r10)r1δmodp1

      (If σ does not appear again, ψ(i+1)[σ] resets to 0).

    • Entering (σ𝒯[i+m]): We must add the new occurrence at the end of the window. Let k be the index of the first occurrence of σ in the new window (if σ is new to the window, k=m1). The new relative offset to add is δnew=(m1)k.

      ψ(i+1)[σ]ψ(i+1)[σ]+r1δnewmodp1

We then add the new contribution to the total hash:

Hi+1𝒯=Hstable+σSactive(r2ψ(i+1)[σ]modp2)(Pstored(σ)Rglob)modp3

This step also takes time O(|Sactive|).

Note:

  1. (1)

    The definition of offset sets comes into its main play here, that is, the update of ψ(i+1)[σ] is computable in O(1) time because of the structure of the offset set.

  2. (2)

    In the computation of the entering characters, we assume knowledge of the first occurrence of σ. We do this by saving linked lists of the locations of each σ and traverse them while shifting, changing pointers to current first occurrence in each list when characters leave the window. For the sake of brevity, we keep the details to a minimum.

Theorem 25 (Transition Complexity).

The transition from Hi𝒯 to Hi+1𝒯 can be computed in time O(1+|𝒯[i]|+|𝒯[i+m]|).

Proof.

Step 2 takes O(1) time. Steps 1 and 3 involve iterating only over the characters present at the leaving position i and the entering position i+m. All internal lookups (offset lists, hash maps) take O(1) time. Thus, the total time is proportional to the number of character occurrences added or removed.

The full algorithm, depicting the above, is detailed in Algorithm 2 in the appendix.

5 Analysis

5.1 Correctness Analysis

We now prove that the hashing scheme correctly identifies matches with high probability.

Theorem 26 (Correctness).

Algorithm 2 reports all valid set-parameterized matches and reports false positives with probability at most n1.

Proof.

The matching algorithm effectively performs a set-string comparison at each of the nm+1 text windows. We analyze the error probability by extending the proof of Theorem 22 by choosing primes: p1=Θ(n2|Σ|2m), p2=Θ(n4|Σ|2m2), and p3=Θ(n2m).

  1. 1.

    Layers 1 & 2 (Structural Collision): By the analysis in Theorem 22, the probability of a collision in the structural hashing (Layers 1 and 2) for any single window is bounded by:

    Pr[struct]Pr[1]+Pr[2]m|Σ|2p1+mp1p2=O(1n2)+O(1n2)=O(1n2).
  2. 2.

    Layer 3 (Rolling Hash Collision): The rolling hash H is a polynomial in r3 of degree at most m. For any single window, using Lemma 16, the collision probability is:

    Pr[roll]mp3=O(mn2m)=O(1n2).
Total Error.

Applying the union bound over all O(n) sliding windows, the total probability of a false positive is:

Pr[Error]n(Pr[struct]+Pr[roll])=nO(1n2)=O(1n).

5.2 Complexity Analysis

Let N=i=1n|𝒯[i]| and M=j=1m|𝒫[j]|. The following theorem summarizes the result. Details appear in the appendix.

Theorem 27 (Complexity).

Algorithm 2 runs in time O(N+M) and uses space O(N+M).

References

  • [1] Amihood Amir, Martin Farach, and S. Muthukrishnan. Alphabet dependence in parameterized matching. Information Processing Letters, 49(3):111–115, 1994. doi:10.1016/0020-0190(94)90086-8.
  • [2] Amihood Amir and Gonzalo Navarro. Parameterized matching on non-linear structures. Information Processing Letters, 109(15):864–867, 2009. doi:10.1016/J.IPL.2009.04.012.
  • [3] Alberto Apostolico, Péter L. Erdős, and Alpár Jüttner. Parameterized searching with mismatches for run-length encoded strings. Theoretical Computer Science, 454:23–29, 2012. doi:10.1016/J.TCS.2012.03.018.
  • [4] Alberto Apostolico, Péter L. Erdős, and Moshe Lewenstein. Parameterized matching with mismatches. Journal of Discrete Algorithms, 5(1):135–140, 2007. doi:10.1016/J.JDA.2006.03.014.
  • [5] Alberto Apostolico and Raffaele Giancarlo. Periodicity and repetitions in parameterized strings. Discrete Applied Mathematics, 156(9):1389–1398, 2008. doi:10.1016/J.DAM.2006.11.017.
  • [6] Brenda S. Baker. A program for identifying duplicated code. In Computing Science and Statistics: Proceedings of the 24th Symposium on the Interface, 1992.
  • [7] Brenda S. Baker. A theory of parameterized pattern matching: Algorithms and applications. In Proceedings of the 25th Annual ACM Symposium on Theory of Computing (STOC), pages 71–80, 1993. doi:10.1145/167088.167115.
  • [8] Brenda S. Baker. Parameterized duplication in strings: Algorithms and an application to software maintenance. SIAM Journal on Computing, 26(5):1343–1362, 1997. doi:10.1137/S0097539793246707.
  • [9] Brenda S. Baker. Parameterized diff. In Proceedings of the 10th Annual ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 854–855. SIAM, 1999. URL: http://dl.acm.org/citation.cfm?id=314500.314968.
  • [10] Richard Beal and Donald A. Adjeroh. Compressed parameterized pattern matching. Theor. Comput. Sci., 609:129–142, 2016. doi:10.1016/J.TCS.2015.09.015.
  • [11] Pierre Bourhis, Aaron Boussidan, and Philippe Gambette. On distances between words with parameters. In 34th Annual Symposium on Combinatorial Pattern Matching, CPM 2023, volume 259 of LIPIcs, pages 6:1–6:23, 2023. doi:10.4230/LIPIcs.CPM.2023.6.
  • [12] Aaron Boussidan. Comparaison automatique de pièces de théâtre à l’aide de distances d’édition et d’alignement. Phd dissertation, Gustave Eiffel University, 2025.
  • [13] Richard Cole, Carmit Hazay, Moshe Lewenstein, and Dekel Tsur. Two-dimensional parameterized matching. ACM Transactions on Algorithms, 11(2):12, 2014.
  • [14] S. Deguchi, F. Higashijima, H. Bannai, S. Inenaga, and M. Takeda. Parameterized suffix arrays for binary strings. In Proceedings of the Prague Stringology Conference, pages 84–94, 2008.
  • [15] Arnab Ganguly, Wing-Kai Hon, Kunihiko Sadakane, Rahul Shah, Sharma V. Thankachan, and Y. Yang. Space-efficient dictionaries for parameterized and order-preserving pattern matching. In 27th Annual Symposium on Combinatorial Pattern Matching (CPM 2016), volume 54 of Leibniz International Proceedings in Informatics (LIPIcs), pages 2:1–2:12. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2016. doi:10.4230/LIPIcs.CPM.2016.2.
  • [16] Arnab Ganguly, Rahul Shah, and Sharma V. Thankachan. pBWT: Achieving succinct data structures for parameterized pattern matching and related problems. In Proceedings of the Twenty-Eighth Annual ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 397–407. SIAM, 2017. doi:10.1137/1.9781611974782.25.
  • [17] R. Garg, R. Prasad, and S. Agarwal. Fast parameterized word matching on compressed text. In International Conference on Computer and Communication Technology (ICCCT), pages 317–321. IEEE, 2014.
  • [18] Carmit Hazay, Moshe Lewenstein, and Dina Sokol. Approximate parameterized matching. ACM Transactions on Algorithms, 3(3):29, 2007. doi:10.1145/1273340.1273345.
  • [19] Richard M. Karp and Michael O. Rabin. Efficient randomized pattern-matching algorithms. IBM J. Res. Dev., 31(2):249–260, 1987. doi:10.1147/RD.312.0249.
  • [20] Orgad Keller, Tsvi Kopelowitz, and Moshe Lewenstein. On the longest common parameterized subsequence. Theoretical Computer Science, 410(51):5347–5353, 2009. doi:10.1016/J.TCS.2009.09.011.
  • [21] R. Khetan, S. Agarwal, and R. Prasad. An efficient approach towards compressed parameterized word matching using wavelet tree. Journal of Information and Optimization Sciences, 37(2):285–301, 2016.
  • [22] I. Lee, J. Mendivelso, and Yoan J. Pinzón. δγ-parameterized matching. In String Processing and Information Retrieval (SPIRE), volume 5280 of LNCS, pages 236–248, 2008.
  • [23] Moshe Lewenstein. Parameterized matching. In Encyclopedia of Algorithms, pages 635–638. Springer, 2008. doi:10.1007/978-0-387-30162-4_282.
  • [24] Juan Mendivelso, Sharma V. Thankachan, and Yoan J. Pinzón. A brief history of parameterized matching problems. Discrete Applied Mathematics, 274:103–115, 2020. doi:10.1016/J.DAM.2018.07.017.
  • [25] Tetsuo Shibuya. Generalization of a suffix tree for RNA structural pattern matching. Algorithmica, 39(1):1–19, 2004. doi:10.1007/S00453-003-1067-9.

Appendix A Proof of Correctness for the Offset Sets

The following is the proof of Lemma 13.

Proof.
Direction ().

Suppose 𝒮1p𝒮2. There exists a bijection π:ΣΣ such that π(𝒮1[i])=𝒮2[i] for all i. This implies that for every σ, the absolute positions of σ in 𝒮1 are identical to the absolute positions of π(σ) in 𝒮2. Consequently, they share the same start position and the same offset set. Thus, the multiset of offset sets at every position remains invariant, and 𝒮1^=𝒮2^.

Direction ().

Suppose 𝒮1^=𝒮2^. We must construct a valid bijection π.

A valid bijection must map a character σ𝒮1 to a character τ𝒮2 if and only if they are structurally identical. Structural identity requires two conditions:

  1. 1.

    They have the same Offset Set O (relative spacing).

  2. 2.

    They have the same Start Position s (absolute placement).

Let N𝒮(s,O) denote the number of distinct characters in string 𝒮 that start at position s and have offset set O. To prove a bijection exists, it is sufficient to show that for all positions s[1..m] and all offset sets O:

N𝒮1(s,O)=N𝒮2(s,O)

If this equality holds, we can map the k characters of type (s,O) in 𝒮1 arbitrarily to the k characters of type (s,O) in 𝒮2. Computing N(s,O) from the Representation: We show that N𝒮(s,O) is uniquely determined by the sequence of multisets 𝒮^. For any position i and offset set O, let Count𝒮(i,O) be the multiplicity of O in the multiset 𝒮^[i]. A character σ contributes to the count of O at position i if:

  1. 1.

    OffSet(σ)=O.

  2. 2.

    σ appears at i. This happens if σ started at some position si such that (is)O.

We can express the total count at position i as the sum of characters starting now (s=i) and characters that started earlier (s<i) and are re-appearing:

Count𝒮(i,O)=N𝒮(i,O)Starts at i+δO,δ>0N𝒮(iδ,O)Started at iδ

Rearranging to solve for the current starts:

N𝒮(i,O)=Count𝒮(i,O)δO,δ>0N𝒮(iδ,O)
Inductive Argument.

We prove N𝒮1(i,O)=N𝒮2(i,O) for all i by strong induction.

  • Base Case (i=𝟏): There are no offsets δ>0 relative to 1. Thus N𝒮(1,O)=Count𝒮(1,O). Since 𝒮1^=𝒮2^, the counts are identical, so N𝒮1(1,O)=N𝒮2(1,O).

  • Inductive Step: Assume N𝒮1(j,O)=N𝒮2(j,O) for all j<i. In the equation for N(i,O):

    • Count(i,O) is identical because 𝒮1^[i]=𝒮2^[i].

    • The summation terms N(iδ,O) involve indices strictly less than i. By the induction hypothesis, these terms are identical for 𝒮1 and 𝒮2.

    Therefore, N𝒮1(i,O)=N𝒮2(i,O).

Since the number of characters for every specific (Start Position, Offset Set) pair is identical, we can construct a perfect bijection π.

Appendix B Proof of Theorem 2

Theorem 28 (Complexity of Comparison Algorithm).

Let M𝒮1=i=1m|𝒮1[i]| and M𝒮2=i=1m|𝒮2[i]| denote the total size (number of character occurrences) of the two set-strings. Algorithm 1 runs in time O(M𝒮1+M𝒮2) and uses space O(M𝒮1+M𝒮2).

Proof.
Time Analysis.
  • Initialization: Selecting primes and initializing tables takes O(|Σ|) time.

  • Offset Set Computation: By Lemma 19, computing the offset sets requires iterating through every character occurrence in both strings exactly once. The time required is O(M𝒮1+M𝒮2).

  • Layer 1 Hashing: We calculate ϕ1(O) for every character σΣ. The cost of computing the hash for a specific offset set O is proportional to its size |O|. Since the sum of the sizes of all offset sets is exactly the total number of character occurrences, the total time is σ|Oσ|=O(M𝒮1+M𝒮2).

  • Layer 2 Hashing: We calculate ϕ2 for every position i[1..m]. The cost at position i is proportional to the number of characters in the set at that position, |𝒮1[i]| (or |𝒮2[i]|). Summing over all positions i, the total time is i=1m|𝒮1[i]|+i=1m|𝒮2[i]|=O(M𝒮1+M𝒮2).

  • Comparison: Comparing the two mashed sequences takes O(m) time, which is dominated by O(M𝒮1+M𝒮2) since mM.

Since |Σ|=O(M𝒮1+M𝒮2) the total running time is O(M𝒮1+M𝒮2).

Space Analysis.

Storing the offset sets and the intermediate hash values requires space proportional to the total number of elements in the sets, which is O(M𝒮1+M𝒮2).

Appendix C Proof of Complexity Analysis

Proof.
Time Complexity.
  1. 1.

    Preprocessing: Building the occurrence lists takes O(N). Computing the initial pattern hash takes O(M).

  2. 2.

    Sliding Window Scan: The loop runs for nm steps. Inside the loop, the “Global Shift” (Step 2) takes O(1) time. The “Isolate” (Step 1) and “Reintegrate” (Step 3) phases iterate only over the set of active characters Sactive=𝒯[i]𝒯[i+m]. Let Cost(i) be the number of operations at step i. We have Cost(i)=O(1+|𝒯[i]|+|𝒯[i+m]|). Summing over all windows:

    Total Time=i=1nmCost(i)=i=1nm(O(1)+|𝒯[i]|+|𝒯[i+m]|)

    Observe that each character occurrence in the text at position k appears in the set 𝒯[i] exactly once (when it leaves the window) and in the set 𝒯[i+m] exactly once (when it enters the window). Therefore:

    i=1nm(|𝒯[i]|+|𝒯[i+m]|)2k=1n|𝒯[k]|=2N
  3. 3.

    Hash Updates: All internal hash updates (multiplications, additions) and list lookups (finding next occurrence δ) take O(1) time.

Thus, the total time complexity is O(N+M).

Space Complexity.

The occurrence lists store every character position, requiring O(N) space. The hash maps for Pstored and ψ store one entry per unique character currently in the window. Since the number of unique characters cannot exceed the number of character occurrences, this is bounded by O(N). The pattern storage takes O(M). Thus, the total space is O(N+M).

Appendix D Algorithms

Algorithm 1 CompareSetStrings(𝒮1,𝒮2).
Algorithm 2 SetParameterizedMatching(𝒫,𝒯).