Dynamic Pattern Matching with Wildcards
Abstract
We study the fully dynamic pattern matching problem where the pattern may contain up to wildcard symbols, each matching any symbol of the alphabet. Both the text and the pattern are subject to updates (insert, delete, change). We design an algorithm with preprocessing and update/query time . The bound is truly sublinear for a constant , and sublinear when . We further complement our results with a conditional lower bound: assuming subquadratic preprocessing time, achieving truly sublinear update time for the case would contradict the Strong Exponential Time Hypothesis (SETH). Finally, we develop sublinear algorithms for two special cases:
-
If the pattern contains non-wildcard symbols, we give an algorithm with preprocessing time and update time , which is truly sublinear whenever is truly sublinear.
-
Using FFT technique combined with block decomposition, we design a deterministic truly sublinear algorithm with preprocessing time and update time for the case that there are at most two non-wildcards.
Keywords and phrases:
pattern matching, wildcards, dynamic algorithms, string algorithms, data structuresCopyright and License:
2012 ACM Subject Classification:
Theory of computation Problems, reductions and completenessEditors:
Meena Mahajan, Florin Manea, Annabelle McIver, and Nguyễn Kim ThắngSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
Pattern matching is a fundamental problem in computer science with applications in text search, bioinformatics, log analysis, and data mining [37, 40, 20, 2, 25, 6, 36, 12, 15, 11, 28]. In its basic form, given a pattern P of length and a text T of length with symbols from alphabet with size , the task is to decide whether P occurs in T as a contiguous substring; that is, whether there exists an index such that This problem has been studied for decades, and many efficient linear-time solutions such as the Knuth–Morris–Pratt and Boyer–Moore algorithms are well known [32, 10].
Traditionally, pattern matching aims to find exact occurrences of a pattern, yet in many practical applications such rigidity is unrealistic [41, 33, 35, 23]. The pattern or the text may contain uncertain or corrupted symbols due to noise, ambiguity, or small variations; for example when accounting for mutations in genomic data. This motivates the study of algorithms that search for substrings close to the pattern under standard distance measures such as Hamming distance [1, 13, 27]. Another common way to model uncertainty is to assume that the positions of the corrupted symbols are known. This is typically handled by introducing a wildcard symbol, which matches any single symbol of the alphabet [22, 26, 29]. This gives rise to the problem of pattern matching with wildcards, where across both the pattern and the text, the total number of wildcard positions is at most . Since each wildcard can stand for any symbol of , a pattern with wildcards implicitly represents up to different strings.
Adding wildcards makes the pattern matching problem harder. With a fixed pattern, the search space is small. For example, let the text be and the pattern be . For this instance, there is only one match. If we instead use , where “?” refers to the wildcard symbol, then every occurrence of C in T becomes a match. This simple change increases the number of matches from to . Thus, even limited uncertainty can drastically increase the complexity of the problem.
A substantial body of work has studied pattern matching with wildcards in either the pattern or the text [29, 8, 5]. For this problem, Fischer and Paterson [22] gave an algorithm, and later Indyk [26] gave a randomized algorithm. Kalai [29] subsequently obtained a randomized algorithm, while the first deterministic algorithm was given by Cole and Hariharan [17].
In this paper, we study pattern matching with wildcards in a fully dynamic setting. Both the text and the pattern may change through insertions, deletions, or substitutions, including updates to wildcard positions. Our goal is to maintain pattern-matching queries in sublinear time. For dynamic pattern matching, most prior work considers settings without wildcards. In these studies, dynamism may arise in several ways: the text may be updated dynamically, the pattern may be updated dynamically, or both may change over time. All three cases have been studied extensively. For dynamic text with a static pattern, Amir et al. [4] presented an algorithm that preprocesses a text of length and a pattern of length in time, reporting all new occurrences of the pattern after each text update in time.
For static text with a dynamic pattern, efficient suffix tree based solutions were introduced by Weiner [43] and later improved by McCreight [34], Ukkonen [42], Farach [19], and others [30]. For a fixed finite alphabet, these algorithms preprocess the text in time and answer pattern queries in time, where is the number of occurrences of the pattern. Finally, in the fully dynamic setting where both text and pattern may change, several algorithms have been developed [24, 21, 38, 3]. For example, Sahinalp and Vishkin [38] support insertions and deletions in time per update, with query times comparable to Weiner’s static algorithm. Alstrup, Brodal, and Rauhe [3] handle insertions, deletions, and substring moves in time per update, and answer pattern searches in time.
Our work extends these dynamic pattern matching studies to consider wildcards. We provide a randomized algorithm that, after preprocessing, supports updates and queries in amortized time which is sublinear for and truly sublinear for constant . We also show that this bound is almost tight: under SETH, no algorithm with subquadratic preprocessing time can achieve truly sublinear update time when . In addition to the general result, we design specialized algorithms for two restricted settings, with the complexity expressed in terms of the number of non-wildcard symbols in the pattern.
-
1.
When the wildcard positions are fixed, we obtain preprocessing in time and support updates in time time, where is the number of non-wildcard symbols.
-
2.
When the pattern contains at most two non-wildcard symbols, we give a deterministic algorithm based on FFT and block decomposition techniques, with preprocessing time and update time .
Organization of the paper
Section 2 gives a high-level overview of our techniques and main results. In Section 3, we introduce formal definitions and preliminaries. The general dynamic pattern matching with wildcards setting is studied in Section 4, followed by algorithms for sparse pattern matching regimes in Section 5. Finally, in Section 6 we establish a conditional lower bound on the update time of any algorithm for the dynamic pattern matching with wildcards problem.
2 Results and Techniques
We study the problem of dynamic pattern matching in the presence of wildcard symbols. Our contributions fall into two main lines.
General setting.
In the most flexible version of the problem, both the text and the pattern may contain wildcards, and updates may insert, delete, or substitute symbols (including wildcards). For this setting, we design randomized algorithms with preprocessing time and update/query time The bound is truly sublinear for constant , and remains sublinear whenever . This gives the first general framework for handling fully dynamic wildcards on both sides of the matching problem.
Sparse patterns.
We also investigate regimes where the number of non-wildcards in the pattern is small. This additional structure enables more efficient algorithms:
-
If the pattern contains at most two non-wildcard symbols, we obtain a deterministic algorithm based on FFT and block decomposition, with preprocessing time and update time . Though the setting might seem simple, this is the most technical part of the paper, with the most challenging component captured by the Range-Pair problem (Problem 5.1), which we solve and use in this regime. The full description and proof of this algorithm are given in the full version.
-
If all wildcards are confined to the pattern and their positions are fixed in advance, we design a randomized algorithm with preprocessing time and update time , where is the number of non-wildcard positions.
Finally, we establish a conditional lower bound on the update and query time for any algorithm solving dynamic pattern matching with wildcards.
Together, these results give a unified picture of the problem’s complexity. Figure 1 highlights the regimes where our upper and lower bounds apply. Also, for completeness and comparison, Table 1 summarizes the state-of-the-art results in pattern matching alongside our contributions. As shown in Table 1, when the pattern contains no wildcards, dynamic exact matching can be solved with preprocessing and polylogarithmic update time [14]. However, as noted, even a single wildcard can increase the number of matches.
| Problem Variant | Preprocessing Time | Algorithm/Query Time |
| Static Matching | ||
| Exact Matching [32, 10] | ||
| Wildcard Matching [22, 26, 29] | ||
| -mismatches(constant ||) [1, 16] | ||
| Dynamic Exact Matching | ||
| Dynamic Text [4] | per update | |
| Fully Dynamic [3] | per query | |
| Dynamic Matching with Wildcards (This Work) | ||
| General (Randomized) [Theorem 4.1] | ||
| Sparse Pattern (at most 2 non-wildcards) [Theorem 5.3] | ||
| Sparse Pattern (fixed wildcards) [Theorem 5.7] | ||
| Conditional Lower Bound [Theorem 6.4] | – | No for |
Our approach builds on a simple but very useful observation. If the pattern contains a symbol that appears only a few times in the text , we can directly check those occurrences in the text and verify the aligned substrings (see Figure 2).
This leads us to a frequency-based classification of symbols: those that occur at most times in are called rare, while the rest are considered frequent. When contains a rare symbol, we verify all possible candidate matches that align that rare symbol.
Given this observation, we can assume without loss of generality that every symbol in the pattern is frequent. In this case, the number of candidate positions in the text that could match the pattern is large. The key insight, however, is that the number of distinct frequent symbols is limited to . This restriction allows us to design an alternative algorithm based on standard data structures.
The main data structure we use in our algorithm is the polynomial rolling hash table. At a high level, it provides a succinct summary of substrings that (i) updates efficiently after a single-symbol change and (ii) lets us handle wildcards. We treat wildcards by partitioning the string into contiguous non-wildcard segments: the hash is computed only over these segments, and two strings match under the wildcard semantics if and only if all corresponding non-wildcard parts have equal hashes. In this way, wildcards are naturally ignored, and correctness reduces to verifying equality of the fixed pieces. To support dynamic updates, we maintain these hashes in balanced BSTs such as treaps [39], which allow insertions, deletions, and substitutions in logarithmic time, and combine them with fully dynamic LCS to handle candidate completions with polylogarithmic overhead [14].
As we discussed earlier, our algorithm achieves truly sublinear update time when the number of wildcards is constant, and remains sublinear as long as the number of wildcards is . A natural question is whether these bounds can be improved further, perhaps matching the polylogarithmic update times known for the static case. Unfortunately, our results suggest otherwise. We show that once the number of wildcards reaches , no algorithm with reasonable preprocessing can achieve truly sublinear update time with subquadratic preprocessing unless the Strong Exponential Time Hypothesis (SETH) fails. This hardness result follows from a reduction from the orthogonal vectors problem.
Given our results, several natural questions remain open:
-
Can we design algorithms with truly sublinear update time when is not constant but still ? For constant , can we further improve the update and query bounds?
-
Can we obtain truly sublinear algorithms for cases where , provided the wildcards have some special structure?
The first direction appears particularly promising for future work. In line with the second, we provide algorithms achieving truly sublinear update times for certain resticted settings. We first consider the setting where the wildcard positions are fixed in advance and the number of non-wildcard symbols is sublinear. In this case, it suffices to maintain rolling hash values only for the non-wildcard positions, which enables sublinear update times for both the pattern and the text.
For the special case where the pattern contains at most two non-wildcard symbols, the static version of the problem can already be solved efficiently by applying FFT: by encoding blocks of the text as polynomials and using convolution, one can align the two characters across the text. In the dynamic setting, however, a single update can change many convolution values, so the FFT method alone is insufficient. To address this, we combine block decomposition with the frequent/rare symbol technique: the text is divided into blocks, only the affected blocks are recomputed after an update, rare symbols are treated explicitly, and frequent ones are handled in the convolution structures. This approach ensures that each update can be processed within sublinear time, achieving preprocessing complexity and query complexity .
3 Preliminaries
We consider a finite alphabet of size . Our input consists of two strings: a text of length , and a pattern of length . Both the text and the pattern together may contain at most wildcard symbols, denoted by ?. A wildcard symbol can match any symbol from the alphabet.
For a string and , we use to denote the substring For example, if , then . We say that P matches a substring () if for every , either , or . The set of all matching positions is denoted by
The concatenation of two strings , , denoted , is defined by appending to the end of . For instance, if and , then .
We classify symbols in as frequent or rare according to a frequency threshold. Let the threshold value be
A symbol is called rare if it occurs fewer than times in T, and frequent otherwise.
For each symbol , we maintain the set which stores all positions where appears in T. In addition to membership queries for , we also require efficient support for lower bound queries, i.e., finding the smallest index in that is greater than or equal to a given value. For handling the updates and queries efficiently, we use balanced binary search trees.
Fact 3.1 (Balanced BST support).
Each set can be maintained under dynamic text updates such that membership queries, lower bound queries, and updates are all supported in time, using a balanced binary search tree [18].
Many of our algorithms are randomized and succeed with high probability. Throughout this paper, we say an event occurs with high probability if it holds with probability at least .
The foundation for our analysis, based on polynomial hashing, is detailed in Handling Matching Query section in the full version. We use a polynomial rolling hash with a prime modulus [31] to efficiently support matching queries. The hash function is formally defined as follows.
Definition 3.2 (Polynomial Rolling Hash).
Let be a string over an ordered alphabet . We define a mapping such that for . The polynomial rolling hash of is
where is chosen uniformly at random from , and is a large prime modulus.
We leverage the following lemma from Handling Matching Query section to prove our probabilistic claims in the paper.
Lemma 3.3.
Let be a collection of pairs of distinct strings, each string drawn from the alphabet and of length at most . Then the probability that there exists at least one pair such that is bounded by
Dynamic Updates
As described earlier, both the pattern and the text may change dynamically. We support the following operations:
-
Pattern Update: Insert, delete, or replace a symbol at position in the pattern, where the new symbol for .
-
Text Update: Insert, delete, or replace a symbol at position in the text, where the new symbol for .
Updates are subject to the constraint that the total number of wildcards in P and T does not exceed .
After each update, the query asks whether there exists an index such that P matches the substring , i.e., whether .
Example 3.4.
Let the alphabet be with . Initially, the pattern is of length , and the text is of length . The pattern contains two wildcards (). The symbol occurrence sets in the text are as follows: By definition, a match occurs at position if , , and (wildcards match any symbol). Checking gives
Now, we modify the first symbol of the pattern from a to b. This produces the new pattern , which still contains exactly two wildcard symbols. If we check the updated pattern against the text, we find that no substring of the text matches, so the set of occurrences becomes empty. Next, we change the first symbol of the text from a to b, resulting in . This modification updates the occurrence sets as follows: Checking the updated text against the current pattern b?b?c shows that a match now exists at position . See Figure 3.
3.1 Queries
Throughout our algorithm, we frequently use two types of queries: the matching query and the longest common substring (LCS) query. Below, we define each query and state the time complexity required to answer it.
Matching Query.
The matching query is defined as which returns true if the pattern substring matches the text substring , allowing wildcards in both P and T.
As we show in Handling Matching Query section in the full version, this query can be answered in time using hash techniques, where denotes the total number of wildcard symbols appearing in segments and . Here we assume that dynamic updates consist only of substitutions, and do not include insertions or deletions.
Lemma 3.5.
The query can be answered with preprocessing time, time per update operation, and time per query, where is the total number of wildcards in and . The algorithm reports a match with probability when one exists, and reports no match with probability when no match exists.
LCS Query.
The LCS query is defined as which returns the length of the longest common substring of two dynamic strings and . By [14], we know that this query can be answered with preprocessing time and time per update.
Theorem 3.6 ([14]).
For strings and , each of length at most , the query can be supported with preprocessing time and time per update operation. Here an update operation may be a substitution, insertion, or deletion applied to either or .
4 Fully Dynamic Pattern Matching with Wildcards
In this section, we consider the general case of dynamic pattern matching with wildcards problem. Note that here both the text T and the pattern P may contain wildcard symbols. We assume that updates consist only of single-character substitutions, and do not include insertions or deletions. This simplifies the presentation of the main ideas. We will later explain in the full version how the algorithm extends to handle insertions and deletions as well.
Our solution is based on maintaining a modified version of the text, denoted by T’, where every rare symbol is replaced with a special placeholder “#”. Therefore, T’ is defined as
Note that the frequency of symbols may change during the dynamic updates. Whenever the frequency of a symbol crosses the threshold , we update T’ by replacing its occurrences with “#” (if it becomes rare) or restoring them to the original symbol (if it becomes frequent). To implement this, we maintain for each symbol the occurrence set . When the text is updated at position from to , we remove from and insert it into . If the frequency of a frequent symbol falls below , we mark its occurrences in T’ with the placeholder “#”; conversely, if a rare symbol becomes frequent, we restore its actual symbol at those positions. Each such change involves only positions, giving an update cost of . The full procedure is formalized in the full version.
The preprocessing phase (Algorithm 1) initializes the data structures, including building T’, maintaining occurrence sets for each symbol, and computing the frequent set F. The algorithm that, after each update, maintains these structures efficiently is explained in the full version.
Given this setup, the matching procedure proceeds differently depending on whether the pattern contains a rare symbol. If it does (Case 1, Algorithm 2), then any valid match must align that rare symbol either with one of its actual occurrences in the text or with one of the wildcard positions in the text. This observation allows us to restrict attention to candidate substrings centered around these positions in T, checking them directly for consistency with the rest of the pattern.
If, on the other hand, the pattern contains no rare symbols (Case 2, Algorithm 3), then each wildcard may be filled with either a frequent symbol or the placeholder “#”. We systematically enumerate all possible completions of both the pattern and the text under this rule and verify each candidate against T’ using efficient LCS queries. Since the number of completions depends only on the number of wildcards and the frequency threshold, this approach avoids recomputation from scratch after updates and remains tractable when the number of wildcards is small.
The overall procedure is summarized in Algorithm 4, and the correctness and efficiency of the algorithm are proved in Theorem 4.1. In the interest of space, the proof of Theorem 4.1 is deferred to the full version.
Theorem 4.1.
Algorithm 4 maintains the dynamic pattern matching with wildcards problem with per-query time
The algorithm always reports a match when one exists, and reports no match with high probability when no match exists.
5 Sparse Pattern
In this section we study sparse patterns, namely patterns in which only a few positions are non-wildcard and the remaining positions are wildcards. Let be the set of non-wildcard indices and let . Such sparse patterns naturally arise in applications such as biological sequence analysis, where only a few positions of a motif are fixed while the rest may vary, or in intrusion detection, where signatures often contain only a few fixed bytes. In this section we consider two regimes: (i) the case where the pattern contains at most two non-wildcard symbols, and (ii) the case where the wildcard positions in the pattern are fixed in advance and the text T contains no wildcards. Throughout this section we work in a substitution-only dynamic model: updates may change symbols in the text or in the pattern, but no insertions or deletions are allowed, so both lengths and (and, in the fixed-wildcard regime, the wildcard index set) remain fixed.
5.1 At Most Two Non-Wildcard Symbols
In this section, we study a special case of Dynamic Pattern Matching with Wildcards under the additional assumption that the pattern P contains at most two non-wildcard symbols at any time. We focus on this substitution-only regime of the problem, and the text T contains no wildcards. But in the remarks we will explain how the algorithm can be extended to handle insertions/deletions in the pattern as well as wildcards in the text. Moreover, in this regime we are also able to count the exact number of matches in the same asymptotic complexity. For consistency with the other sections we only present the decision version in the main algorithm, but counting can also be supported. The detailed explanations of these remarks are given in the full version. For this setting, we design an algorithm with preprocessing time , pattern update time , text update time , and query time .
A general observation in sparse patterns is that leading or trailing runs of ? can be ignored when reasoning about matches. These wildcards only shift the range of permissible starting positions but do not affect the relative positions among the concrete symbols. Consequently, we can conceptually divide any pattern into three parts: a prefix of wildcards, a block of concrete symbols (the query block), and a suffix of wildcards:
After removing the leading and trailing wildcards, let the remaining concrete positions in P be with symbols . A substring matches P if and only if
Equivalently, a match occurs when the concrete symbols appear in the text at positions that preserve the relative offsets . This perspective allows match queries to be reduced to checking whether these symbols appear together at the required distances within the allowed range of starting positions.
In the special case where the pattern P contains at most two non-wildcard symbols, the matching task reduces to checking whether two symbols occur in the text at a specific distance from each other. This motivates the following formal problem.
Problem 5.1 (Dynamic Range-Pair Query).
Let be an alphabet of size . The input is a dynamic string . The task is to support the following operations:
-
: update the symbol at position to .
-
: return the number of indices such that and , where and .
This setting is closely related to the classical Gapped String Indexing problem [9].
In Gapped String Indexing, one preprocesses a static string and answers queries given by two patterns and a gap interval , asking whether there exist occurrences at positions and such that [9].
In the special case where and , this reduces to deciding or counting occurrences of two symbols whose positions differ by exactly .
The Dynamic Range-Pair Query problem captures this special case while additionally restricting valid pairs to lie within a query range , and allowing the underlying text to be updated dynamically.
Viewed this way, the problem can also be interpreted as a dynamic, range-restricted variant of Shifted Set Intersection (equivalent to 3SUM indexing in the static setting) [9, 7].
To tackle this problem, we use a block decomposition approach.
The text is partitioned into consecutive blocks, and for each block we maintain precomputed counts of symbol pairs whose left endpoint lies inside the block.
Updates are handled lazily: when only a small number of positions in a block are modified, we record these changes explicitly and leave the precomputed counters unchanged; once the number of pending changes exceeds a fixed threshold, the block is rebuilt from scratch.
For a query range , blocks that lie entirely inside the query interval – referred to as full blocks – are answered using their precomputed counters, with a correction for any pending updates stored for those blocks.
Only positions belonging to blocks that intersect the query interval partially – namely, the boundary blocks at the left and right ends of the range – are handled by explicit scanning.
Pseudocode of Range-Pair data structure is available in this version, but for the full algorithm explaination and technical details of the data structure, please see the full version.
Theorem 5.2.
By combining the preprocessing procedure (Algorithm 5), the update procedure (Algorithm 7), and the query procedure (Algorithm 6), the data structure answers the Dynamic Range-Pair Query Problem (Problem 5.1) on with:
-
Preprocessing time
-
per-query time
-
amortized update time
-
space usage
Here and are configurable parameters, fixed in advance, that must satisfy .
Now for the main problem, the algorithm distinguishes the possible cases depending on how many non-wildcard symbols appear in the pattern. If the pattern contains a rare symbol, then all its occurrences in the text can be inspected directly, and since at most one further non-wildcard position needs to be checked, the query runs in time. If the pattern contains only frequent symbols, three subcases arise. With no non-wildcards the pattern trivially matches any substring of length (a constant-time check). With exactly one non-wildcard, the query reduces to checking whether that symbol appears in the valid range of positions, which can be done using the set in time. Finally, with two non-wildcards the problem reduces to asking whether two symbols appear in the text at the required distance, which is exactly the Range-Pair Problem defined earlier. To support this reduction we maintain a mapping that assigns all rare symbols to 0 and gives each frequent symbol a distinct nonzero code, so that the mapped text serves as input to the Range-Pair structure. The procedures for preprocessing, queries, and updates are summarized in Algorithms 8, 9, and 10. By Theorem 5.2, with parameters , , and , the resulting bounds match those stated in Theorem 5.3. For the complete algorithms and detailed calculations we refer to the proof in the full version.
Theorem 5.3.
Combining the preprocessing in Algorithm 8, the query procedure in Algorithm 9, and the text/pattern updates in Algorithm 10, the data structure answers the at-most-two non-wildcard dynamic wildcard matching variant with the following guarantees (for ):
-
preprocessing time ,
-
pattern-update time ,
-
text-update time (amortized),
-
per-query time .
5.2 Fixed Wildcard Positions in Pattern
In this subsection we study the dynamic pattern matching problem with fixed wildcards: all wildcard symbols appear only in the pattern P, their index set is known in advance and immutable, and the text T contains no wildcards. Both P and T evolve only via substitution updates, and insertions and deletions are not allowed; therefore updates to P are confined to the solid coordinates , while updates to T may change any . We parameterize by the number of non-wildcard positions and target the sparse regime . To handle this setting, we adopt a masked polynomial rolling hash that ignores the fixed wildcard indices: equivalently, we view P as a concatenation of solid blocks separated by wildcards and compute the hash only over these blocks, enabling comparison against substrings of T under substitutions while treating wildcard coordinates as don’t-cares; this leads to the following definition.
Definition 5.4.
Let be a string of length , and let P be a pattern with wildcard positions , where each is the index of the -th wildcard in P.We define
where is the polynomial rolling hash from Definition 3.2.
The value can be computed in time by concatenating the precomputed hash values of the non-wildcard intervals. Moreover, after each update in , the value of can be updated in time. Let us now define an auxiliary vector , where
so that stores the hash of the length- substring of T starting at position , with the fixed wildcards of P ignored.
Lemma 5.5.
If the pattern P decomposes into non-empty intervals separated by its wildcard positions, then the vector can be constructed in time.
Together, Definition 5.4 and Lemma 5.5 provide the key ingredients for efficient preprocessing in the sparse-pattern setting. The modified hash ensures that wildcards are ignored, while the vector stores precomputed values for all substrings of T that need to be compared against P. The lemma guarantees that this preprocessing can be done in time, where is the number of non-empty intervals in P. Algorithm 11 then presents the full method for handling updates. Intuitively, when the pattern has only a few fixed symbols and the rest are wildcards, we can ignore wildcard positions in all computations. Let be the indices of the non-wildcard positions in P. For every window of the text, we compute based only on the symbols aligned with . We also maintain the single value . A match exists iff appears among ; this can be checked efficiently with a membership query, e.g., in time using a balanced BST.
If the pattern changes at a non-wildcard position, we simply recompute and test membership once. If the text changes at position , only those windows whose alignment uses at one of the non-wildcard positions can be affected. Equivalently, for each with , only must be recomputed. Thus a single text update triggers at most window-hash updates.
Example 5.6.
Let with and non-wildcard indices . Let with . Consider the alignment starting at (i.e., substring ). The two non-wildcard checks are:
so this alignment matches. Accordingly, equals .
Now suppose we update the text at position , changing from a to x. Only the window hashes whose aligned non-wildcard positions use can change: for we must recompute ; for we must recompute . In particular, will no longer equal since no longer matches . See Figure 4 for a visualization of the alignment at , the update at , and the affected window hashes.
Theorem 5.7.
Algorithm 11 maintains the dynamic pattern matching problem with fixed wildcards for a pattern P of length containing at most non-wildcard positions. It requires preprocessing time and supports updates to either P or T in time per update. The algorithm always detects a match correctly when one exists. When no match exists, it reports “no match” with high probability.
The general statement above can be specialized to different regimes of sparsity. First, if the number of non-wildcard positions is bounded by a sublinear power of , we obtain the following corollary.
Corollary 5.8.
If the number of non-wildcard positions satisfies for some constant , then Algorithm 11 runs in preprocessing time and supports updates in time.
Moreover, since the number of non-wildcard positions can never exceed the pattern length , we can directly derive the following bound.
Corollary 5.9.
Since , the algorithm also admits the trivial upper bound obtained by setting . In this case, Algorithm 11 requires preprocessing time and update time .
6 Hardness of Dynamic Pattern Matching with Wildcards
In this section, we establish a conditional lower bound for Dynamic Pattern Matching with Wildcards through a reduction from the Orthogonal Vectors problem. Our goal is to show that any significant improvement in the query time for Dynamic Pattern Matching with Wildcards would imply a breakthrough for Orthogonal Vectors , which is widely believed to be unlikely under the Strong Exponential Time Hypothesis ().
Problem 6.1 (The Orthogonal Vectors Problem).
Given a set of vectors from , the Orthogonal Vectors problem asks whether there exists a pair of distinct vectors such that their inner product is zero:
That is, no coordinate position contains a in both and .
Williams [44] establishes the following connection between Orthogonal Vectors and .
Theorem 6.2 ([44]).
If there exists such that, for all constants , the Orthogonal Vectors problem on a set of vectors of dimension can be solved in time, then is false.
We now show a reduction of Orthogonal Vectors to Dynamic Pattern Matching with Wildcards. This reduction forms the backbone of our lower bound.
Lemma 6.3.
An instance of Orthogonal Vectors with a set of vectors in dimension can be reduced to an instance of Dynamic Pattern Matching with Wildcards with pattern length and text length . If Dynamic Pattern Matching with Wildcards can be solved with preprocess time and query time , then Orthogonal Vectors can be solved in time.
Proof.
Let , where each is a -dimensional binary vector. For each vector , construct a modified vector by replacing every in with , and every in with a wildcard symbol. Define the pattern and text as
where is a delimiter not in . By construction, P has length and T has length . We claim that P matches a substring of T if and only if the Orthogonal Vectors instance contains an orthogonal pair. Indeed, suppose matches in T. Then, for each coordinate :
-
If , then . Since can only match , we must have .
-
If , then , which matches both and .
Thus, there is no coordinate where both and , which means and are orthogonal. The converse follows by reversing this reasoning.
To check all pairs, we iteratively transform the pattern into each for . Each transformation requires at most symbol changes, followed by a query to check if the pattern matches a substring of T. Over all vectors, this yields updates and queries. Each query costs , so the total runtime is
The reduction allows us to transfer lower bounds from Orthogonal Vectors to Dynamic Pattern Matching with Wildcards.
To keep the presentation concise, we defer the proof of Theorem 6.4 to the full version.
Theorem 6.4.
Assuming holds, the fully dynamic wildcard pattern matching problem with wildcards cannot be solved with preprocessing time and query time for any constants .
References
- [1] Karl Abrahamson. Generalized string matching. SIAM journal on Computing, 16(6):1039–1051, 1987. doi:10.1137/0216067.
- [2] V. Abrishami, A. Zaldívar-Peraza, J. M. de la Rosa-Trevín, J. Vargas, J. Otón, R. Marabini, Y. Shkolnisky, J. M. Carazo, and C. O. S. Sorzano. A pattern matching approach to the automatic selection of particles from low-contrast electron micrographs. Bioinformatics, 29(19):2460–2468, 2013. doi:10.1093/BIOINFORMATICS/BTT429.
- [3] Stephen Alstrup, Gerth Stølting Brodal, and Theis Rauhe. Pattern matching in dynamic texts. In Proceedings of the eleventh annual ACM-SIAM symposium on Discrete algorithms, pages 819–828, 2000. URL: http://dl.acm.org/citation.cfm?id=338219.338645.
- [4] Amihood Amir, Gad M Landau, Moshe Lewenstein, and Dina Sokol. Dynamic text and static pattern matching. ACM Transactions on Algorithms (TALG), 3(2):19–es, 2007.
- [5] Amihood Amir, Moshe Lewenstein, and Ely Porat. Faster algorithms for string matching with k mismatches. Journal of Algorithms, 50(2):257–275, 2004. doi:10.1016/S0196-6774(03)00097-X.
- [6] Mika Amit, Rolf Backofen, Steffen Heyne, Gad M. Landau, Mathias Möhl, Christina Otto, and Sebastian Will. Local exact pattern matching for non-fixed rna structures. IEEE/ACM Transactions on Computational Biology and Bioinformatics, 11(1):219–230, 2014. doi:10.1109/TCBB.2013.2297113.
- [7] Boris Aronov, Jean Cardinal, Justin Dallant, and John Iacono. A general technique for searching in implicit sets via function inversion. In 2024 Symposium on Simplicity in Algorithms (SOSA), pages 215–223. SIAM, 2024. doi:10.1137/1.9781611977936.20.
- [8] Gabriel Bathie, Panagiotis Charalampopoulos, and Tatiana Starikovskaya. Pattern matching with mismatches and wildcards. arXiv preprint arXiv:2402.07732, 2024. doi:10.48550/arXiv.2402.07732.
- [9] Philip Bille, Inge Li Gørtz, Moshe Lewenstein, Solon P Pissis, Eva Rotenberg, and Teresa Anna Steiner. Gapped string indexing in subquadratic space and sublinear query time. arXiv preprint arXiv:2211.16860, 2022. doi:10.48550/arXiv.2211.16860.
- [10] Robert S. Boyer and J. Strother Moore. A fast string searching algorithm. Communications of the ACM, 20(10):762–772, 1977. doi:10.1145/359842.359859.
- [11] Vincent Bushong, Russell Sanders, Jacob Curtis, Mark Du, Tomas Cerny, Karel Frajtak, Miroslav Bures, Pavel Tisnovsky, and Dongwan Shin. On matching log analysis to source code: A systematic mapping study. In Proceedings of the International Conference on Research in Adaptive and Convergent Systems (RACS), pages 181–187, 2020.
- [12] J. V. Capacho, A. Subias, L. Trave-Massuyès, and F. Jimenez. Alarm management via temporal pattern learning. Engineering Applications of Artificial Intelligence, 65:506–516, 2017. doi:10.1016/J.ENGAPPAI.2017.07.008.
- [13] Timothy M Chan, Ce Jin, Virginia Vassilevska Williams, and Yinzhan Xu. Faster algorithms for text-to-pattern hamming distances. In 2023 IEEE 64th Annual Symposium on Foundations of Computer Science (FOCS), pages 2188–2203. IEEE, 2023. doi:10.1109/FOCS57990.2023.00136.
- [14] Panagiotis Charalampopoulos, Paweł Gawrychowski, and Karol Pokorski. Dynamic Longest Common Substring in Polylogarithmic Time. In Artur Czumaj, Anuj Dawar, and Emanuela Merelli, editors, 47th International Colloquium on Automata, Languages, and Programming (ICALP 2020), volume 168 of Leibniz International Proceedings in Informatics (LIPIcs), pages 27:1–27:19, Dagstuhl, Germany, 2020. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.ICALP.2020.27.
- [15] Y. Cheng, I. Izadi, and T. Chen. Pattern matching of alarm flood sequences by a modified smith–waterman algorithm. Chemical Engineering Research and Design, 91(6):1085–1094, 2013.
- [16] Peter Clifford and Raphaël Clifford. Simple deterministic wildcard matching. Inf. Process. Lett., 101(2):53–54, 2007. doi:10.1016/J.IPL.2006.08.002.
- [17] Richard Cole and Ramesh Hariharan. Verifying candidate matches in sparse and wildcard matching. In Proceedings of the 34th Annual ACM Symposium on Theory of Computing (STOC ’02), pages 592–601. ACM, 2002. doi:10.1145/509907.509992.
- [18] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms. MIT Press, 4th edition, 2022.
- [19] Martin Farach. Optimal suffix tree construction with large alphabets. In Proceedings 38th Annual Symposium on Foundations of Computer Science, pages 137–143. IEEE, 1997. doi:10.1109/SFCS.1997.646102.
- [20] Simone Faro and Thierry Lecroq. The exact online string matching problem: A review of the most recent results. ACM Computing Surveys, 45(2), 2013. doi:10.1145/2431211.2431212.
- [21] Paolo Ferragina and Roberto Grossi. Fast incremental text editing. In Proceedings of the sixth annual ACM-SIAM symposium on Discrete algorithms, pages 531–540, 1995. URL: http://dl.acm.org/citation.cfm?id=313651.313815.
- [22] Michael J. Fischer and Michael S. Paterson. String matching and other products. In Richard M. Karp, editor, Complexity of Computation, volume 7 of SIAM-AMS Proceedings, pages 113–125, Providence, RI, 1974. AMS.
- [23] Zvi Galil and Kunsoo Park. An improved algorithm for approximate string matching. SIAM Journal on Computing, 19(6):989–999, 1990. doi:10.1137/0219067.
- [24] Ming Gu, Martin Farach, and Richard Beigel. An efficient algorithm for dynamic text indexing. In Proceedings of the Fifth Annual ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 697–704, 1994. URL: http://dl.acm.org/citation.cfm?id=314464.314675.
- [25] Saqib Iqbal Hakak, Amirrudin Kamsin, Palaiahnakote Shivakumara, Gulshan Amin Gilkar, Wazir Zada Khan, and Muhammad Imran. Exact string matching algorithms: Survey, issues, and future research directions. IEEE Access, 7:69614–69637, 2019. doi:10.1109/ACCESS.2019.2914071.
- [26] Piotr Indyk. Faster algorithms for string matching problems: Matching the convolution bound. In 39th Annual Symposium on Foundations of Computer Science (FOCS), pages 166–173, Washington, DC, USA, 1998. IEEE Computer Society. doi:10.1109/SFCS.1998.743440.
- [27] Ce Jin and Yinzhan Xu. Shaving logs via large sieve inequality: Faster algorithms for sparse convolution and more. In Proceedings of the 56th Annual ACM Symposium on Theory of Computing, pages 1573–1584, 2024. doi:10.1145/3618260.3649605.
- [28] Michael C. Johannesmeyer, Ashish Singhal, and Dale E. Seborg. Pattern matching in historical data. AIChE Journal, 48(9):2022–2038, 2002.
- [29] Adam Kalai. Efficient pattern-matching with don’t cares. In 13th Annual ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 655–656, Philadelphia, PA, USA, 2002. Society for Industrial and Applied Mathematics. URL: http://dl.acm.org/citation.cfm?id=545381.545468.
- [30] Juha Kärkkäinen and Peter Sanders. Simple linear work suffix array construction. In International colloquium on automata, languages, and programming, pages 943–955. Springer, 2003. doi:10.1007/3-540-45061-0_73.
- [31] Richard M. Karp and Michael O. Rabin. Efficient randomized pattern-matching algorithms. IBM Journal of Research and Development, 31(2):249–260, 1987. doi:10.1147/RD.312.0249.
- [32] Donald E. Knuth, James H. Morris, and Vaughan R. Pratt. Fast pattern matching in strings. SIAM Journal on Computing, 6(2):323–350, 1977. doi:10.1137/0206024.
- [33] Gad M Landau and Uzi Vishkin. Fast parallel and serial approximate string matching. Journal of Algorithms, 10(2):157–169, 1988. doi:10.1016/0196-6774(89)90010-2.
- [34] Edward M McCreight. A space-economical suffix tree construction algorithm. Journal of the ACM (JACM), 23(2):262–272, 1976. doi:10.1145/321941.321946.
- [35] Gonzalo Navarro. A guided tour to approximate string matching. ACM Computing Surveys, 33(1):31–88, 2001. doi:10.1145/375360.375365.
- [36] Peyman Neamatollahi, Montassir Hadi, and Mahmoud Naghibzadeh. Simple and efficient pattern matching algorithms for biological sequences. IEEE Access, 8:23838–23846, 2020. doi:10.1109/ACCESS.2020.2969038.
- [37] P. Pandiselvam, T. Marimuthu, and R. Lawrance. A comparative study on string matching algorithms of biological sequences. arXiv preprint arXiv:1401.7416, 2014. arXiv:1401.7416.
- [38] Süleyman Cenk Sahinalp and Uzi Vishkin. Efficient approximate and dynamic matching of patterns using a labeling paradigm. In Proceedings of 37th Conference on Foundations of Computer Science, pages 320–328. IEEE, 1996.
- [39] Raimund Seidel and Cecilia R Aragon. Randomized search trees. Algorithmica, 16(4):464–497, 1996. doi:10.1007/BF01940876.
- [40] Muhammad Tahir. Efficient pattern matching algorithm for dna sequences. Expert Systems with Applications, 82:96–112, 2017.
- [41] Esko Ukkonen. Algorithms for approximate string matching. Information and Control, 64(1-3):100–118, 1985. doi:10.1016/S0019-9958(85)80046-2.
- [42] Esko Ukkonen. On-line construction of suffix trees. Algorithmica, 14(3):249–260, 1995. doi:10.1007/BF01206331.
- [43] Peter Weiner. Linear pattern matching algorithms. In 14th Annual Symposium on Switching and Automata Theory (swat 1973), pages 1–11. IEEE, 1973. doi:10.1109/SWAT.1973.13.
- [44] Ryan Williams. A new algorithm for optimal 2-constraint satisfaction and its implications. Theoretical Computer Science, 348(2-3):357–365, 2005. doi:10.1016/J.TCS.2005.09.023.
