Periodicity Property Testing
on Strings with Wildcards
Abstract
In this work, we study periodicity in strings with wildcards. A string with at most wildcards is called strongly -periodic if the wildcards in can be replaced with alphabet symbols to obtain a string with period , and weakly -periodic if matches for all . Intuitively, both generalize to -periodicity, which is the property of being -periodic for some .
An -tester for a property is an algorithm that distinguishes between strings that satisfy and strings where one needs to change at least an -fraction of the symbols to obtain a string that satisfies . We study one-sided error testers, where strings satisfying must always be accepted, while strings that are -far must be rejected with probability at least . The complexity of a tester is the worst-case number of symbols of an input of length it must read to make the decision.
We design the following testers for :
-
1.
An -tester for strong -periodicity with complexity 111Hereafter, and means that we hide factors polylogarithmic in and polynomial in ..
-
2.
An -tester for strong -periodicity with complexity .
-
3.
An -tester for weak -periodicity with complexity .
-
4.
An -tester for weak -periodicity with complexity .
Additionally, we show a lower bound on the complexity of -testers for weak -periodicity, implying that our tester for weak -periodicity is optimal up to a multiplicative factor for a wide range of and . Finally, our tester for strong -periodicity generalizes the one of [Lachish and Newman; Algorithmica 2011] for strings without wildcards, matching (up to polylogarithmic factors) the unconditional lower bound of in said work for constant .
Keywords and phrases:
periodicity, property testing, wildcardsCopyright and License:
Tatiana Starikovskaya; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Probabilistic computation ; Mathematics of computing Combinatorics on wordsFunding:
Parts of the presented ideas were conceived during a research visit funded by a Royal Society International Exchanges Award. Taha El Ghazi, Jonas Ellert, and Tatiana Starikovskaya were partially funded by grant ANR20-CE48-0001 from the French National Research Agency. Jonas Ellert was partially supported by an ERCIM Alain Bensoussan fellowship.Editors:
Philip Bille and Nicola PrezzaSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
In this work, we study property testers for periodicity in the presence of wildcards. An -tester is an algorithm that, given an input string, must decide whether it satisfies a given property, while querying as few symbols of the input as possible. Often, it is impossible to make an exact decision without querying the entire input; instead, one wants to distinguish between strings that have the property and those that are far from having the property, where far means that one has to change an -fraction of the symbols of the input to obtain a string satisfying the property (for a given ). The tester does not access the input directly and instead uses oracle queries of the form: what is the symbol at a given position? The number of queries a tester makes is referred to as its complexity. We study one-sided error testers, which always accept strings satisfying the property, while rejecting strings that are -far from having the property with probability at least .
A string without wildcards has a period if . Property testing for periodicity in such strings has been previously studied in [9, 10] and [15]. Ergün et al. [9, 10] studied the problem using three generalizations of periodicity that lead to different interpretations of approximate periodicity. They show that the resulting definitions of approximate periodicity are constant approximations of each other and that it is possible to distinguish between the case when has an -approximate period and is -far from having a period using queries, even for constant . Lachish and Newman [15] showed a property tester that decides whether a string has a period and has complexity . They further showed that, to decide whether there is a period for , it suffices to query symbols.
In this work, we focus on strings with wildcards. A wildcard, denoted by , matches all symbols from the alphabet, intuitively representing a symbol whose exact value is irrelevant or unavailable. We consider two natural extensions of the notion of periodicity to strings containing wildcards: strong periodicity and weak periodicity.
A string with at most wildcards is said to have a strong period if there exists a wildcard-free string that matches and has period . Equivalently, has a strong period if its wildcards can be replaced by alphabet symbols so that the resulting string satisfies for all .
For weak periodicity, we relax this requirement: we say that has a weak period if for all , where if and only if or at least one of and is a wildcard. For example, is weakly -periodic (but not strongly). Both notions of periodicity with wildcards have previously been studied extensively in the context of generalizing the Fine–Wilf periodicity lemma [11]; see [2, 3, 4, 5, 6, 7, 19, 14, 18, 20].
Our -testers.
For a string , integers , , and , and a value , we design the following testers:
-
1.
An -tester for strong -periodicity with complexity ; for , we achieve complexity . See Lemma 9.
-
2.
An -tester for strong -periodicity with complexity ; for , we achieve complexity . See Lemma 10.
-
3.
An -tester for weak -periodicity with complexity . See Corollary 23.
-
4.
An -tester for weak -periodicity with complexity . See Corollary 24.
The starting point of our testers is a conceptual partition of the input string into equivalence classes for a fixed period . Each equivalence class is a subsequence of consisting of the symbols of whose positions are in the same class modulo . If is strongly -periodic, then every equivalence class must be unary. Intuitively, a witness against strong periodicity is a pair of positions in an equivalence class in which the symbols do not match. On a very high level, we show that for far instances the number of such witnesses is large, which allows detecting them with a few queries. For weakly -periodic strings, every equivalence class contains at most maximal unary factors separated by wildcards. Here, a witness consists of non-matching pairs that do not overlap. Intuitively, each wildcard can “fix” one such pair (for example, consider which is weakly -periodic and has two such pairs), but we would need wildcards to fix non-overlapping non-matching pairs, which we simply do not have. To show that the number of such witnesses in far instances is large, we demonstrate that a far instance can be decomposed into factors, where each factor is far from unary. We then need to find one non-matching pair in each factor, which can be done with a few queries.
Lower bounds.
Lachish and Newman [15] showed a lower bound on the complexity of testers for periodicity on strings without wildcards. Namely, they showed that every -tester for -periodicity must query symbols of the input, for every . This implies that, for constant and , the tester in Lemma 10 is optimal up to polylogarithmic factors.
Finally, in Theorem 31, we show a lower bound on the complexity of testers for weak -periodicity. In a simplified form, it states that any -tester for weak -periodicity has complexity . In the regime when and are sufficiently small, for example, when , our tester for weak -periodicity (Corollary 24) matches this bound up to polylogarithmic factors.
Other related work.
Awofeso et al. [1] studied property testing for quasiperiodicity. The quasiperiodicity studied in this work is a cover, where they presented an algorithm that can decide if a string has a cover of length using queries. Chan et al. [8] and then Jin and Kociumaka [13] studied property testers for pattern matching.
2 Preliminaries
For integers , we write to denote , and to denote . Let Primes denote the set of prime numbers.
An alphabet is a finite set whose elements are called symbols. We consider a special symbol called a wildcard and denote by . For two symbols we say that matches and write if either and , or at least one of and is a wildcard. Otherwise, we say that and do not match and write .
A length- string over an alphabet is a sequence of symbols from . We write to denote that is a string of length . For , we write to denote the -th symbol in , and to denote the sequence if , and the empty string otherwise. is called a factor of . We say that is a -wildcard string if it contains at most wildcards.
Consider two strings . We say that and match and write if, for all , .
Strong and weak periods.
We now give formal definitions of strong and weak periods of strings containing wildcards. Intuitively, a string is strongly periodic if we can replace all its wildcards with symbols from to obtain a periodic string in .
Definition 1 (Strongly periodic string).
Let , , and . We say that is strongly -periodic or that has a strong period if contains at most wildcards and there is a string such that , where is an infinite string obtained by concatenating infinitely many copies of .
Weak periodicity is a more relaxed notion that, intuitively, allows wildcards to act as transition points.
Definition 2 (Weakly periodic string).
Let , , and . We say that is weakly -periodic or that has a weak period if and only if contains at most wildcards and .
Property testers.
In the property testing model, the goal is to decide whether the input has a certain property or is far from having it by querying as few of the input symbols as possible. We next formalize what it means for a string to be far from having some property.
Definition 3 (-far).
We say that a string is -far from having a property if one needs to change at least symbols of to obtain a string that has property .
Definition 4 (Property tester).
A one-sided -property tester for a property is an algorithm that receives as an input a string and:
-
returns “yes” if satisfies the property with probability ,
-
returns “no” with probability at least if is -far from satisfying the property,
-
returns “yes” or “no” otherwise.
In this work, we focus on non-adaptive property testers, meaning that the symbols to query are selected offline, i.e. the, position of the -th queried symbol does not depend on the first queried symbols. Consistent with the literature, we define the complexity of a tester as the number of symbols of the input string that they query in the worst case.
Structure of strings with wildcards.
Before describing periodicity property testers, we first introduce additional notation used throughout the paper. For analyzing whether a string is -periodic for some and , we define the following strings : for , we have , i.e., is obtained by restricting to positions equivalent to . We refer to strings as equivalence classes.
We will analyze (factors of) equivalence classes via symbol frequencies. For any string and any symbol , we define to be the number of occurrences of in . If , then we further define , i.e., the number of occurrences of symbols that are neither nor . The majority symbol of is that maximizes ; if multiple symbols satisfy this definition, then we choose the one that occurs first in . For this uniquely defined majority symbol , we write and . All other symbols are called minority symbols.
Proposition 5.
For every string , there is a bipartition of such that and .
Proof.
Let be the majority symbol of . We start with empty sets and and process the symbols in in an arbitrary order. We add each symbol we encounter to the set that currently contributes fewer positions to . Clearly, at any point in time, the terms and differ by at most . We assume, without loss of generality, that in the end we have , meaning that . Finally, we insert to . Then, we have , which concludes the proof since and imply that .
3 Strong Periodicity
We start by introducing the notion of witnesses of strong non-periodicity.
Definition 6 (Witnesses).
Let , and . We say that is a -witness if and . We denote the set of all -witnesses of by .
Observation 7.
A string is strongly -periodic if and only if it is a -wildcard string and .
In strings that are far from periodic the number of -witnesses is large:
Lemma 8.
Let and be a -wildcard string that is -far from being strongly -periodic. We have . Further, if , then .
Proof.
Case I (unbounded ).
There are at least positions such that and is a minority symbol of . Otherwise, we could simply replace each minority symbol to obtain a strongly -periodic string, contradicting the fact that is -far. Let us fix such a position , and set and . Our fixed symbol can be paired with each symbol from in to give a witness pair, and the number of such symbols is . In particular, by summing over all minority symbols from , we conclude that each equivalence class contributes at least unordered pairs, resulting in -witnesses (where we divide by to avoid double-counting). Then, by the Cauchy–Schwarz inequality, we have .
Case II ().
The bound on implies that there are classes with wildcards. The combined length of these classes is . We claim that there are positions such that is not the majority symbol of and . Otherwise, we could simply overwrite the at most classes that have wildcards with unary strings, using substitutions, and then replace the minority symbols in the remaining classes with the majority symbol using another substitutions. This contradicts that is -far from being strongly -periodic. Among the at least positions specified above, fix a position , and let and . The symbol can be paired with every symbol from in to give a -witness. The number of such symbols is . Thus, in total, we have -witnesses (we have again divided by to avoid double-counting).
3.1 Strong Periodicity Testers
We now show testers for strong periodicity.
Lemma 9.
For all and , Algorithm 1 is a one-sided -property tester for strong -periodicity with complexity . The complexity is if .
Proof.
The algorithm never returns “no” if it receives a strongly -periodic string by Observation 7. Next, consider a -wildcard string that is -far from being strongly -periodic. Since the number of pairs such that is at most , the probability that a randomly selected pair belongs to is at least . Hence, the probability that our algorithm returns “yes” is at most , where the penultimate step is due to Lemma 8.
We now generalize the algorithm to test for strong -periodicity. A key observation from the algorithm above is that roughly independent pairs of positions suffice to discover a -witness. By sampling around positions, we can still form roughly pairs. By slightly increasing the sampling rate, we can account for the fact that the pairs are not entirely independent, and also boost the success probability enough to succeed for all with high enough probability. This results in the lemma below, whose proof is provided in Appendix B.
4 Weak Periodicity
We first introduce the notion of witnesses for weak periodicity. For Lemma 14 below, we essentially apply Proposition 5 to each class of that does not contain too many wildcards.
Definition 12.
Let be a -wildcard string and .
-
A -witness is a pair such that and .
-
An explicit -witness is a -witness such that .
-
An implicit -witness is a set of pairs such that
-
–
for each , either and , or is a -witness, and
-
–
for each distinct , if then (disjointness condition).
-
–
Lemma 13.
Weakly -periodic strings admit neither explicit nor implicit -witnesses.
Proof.
Assume that some string is weakly -periodic. In this case, it does not contain explicit -witnesses because for all . Furthermore, assume towards a contradiction that there exists an implicit -witness . For every such that and , we obviously have for . For every which is a -witness, there exists with and (as otherwise the class cannot be weakly -periodic). By the disjointness condition, all are distinct, and hence contains wildcards, a contradiction.
Lemma 14.
For , consider a -wildcard string and . If is -far from being weakly -periodic, then there is a set and, for each , sets satisfying:
-
for every and , ;
-
, and, for every , ;
-
if each position in is sampled independently with probability , the sample hits (resp. ) independently with probability at least (resp. ).
Proof.
Consider the equivalence classes . For each , define the set to contain the positions corresponding to the minority symbols of , i.e., . We have . Otherwise, we could merely replace all minority symbols with the majority ones to obtain a -periodic string, contradicting the fact that is -far. Let be the set containing exactly the indices such that contains at most wildcards. Note that there are at most classes that contain more than wildcards and they contribute at most minority elements. Furthermore, (where the second step is due to ). We have shown that . For each class with , by Proposition 5, we can bipartition its positions into sets and of sizes and such that every pair in yields a mismatch in . For the final point, all sets are disjoint and thus hit independently. We observe that . Hence, . The probability of not missing is at least (using ). The proof for is symmetric.
4.1 Testing Weak -Periodicity Through Implicit Witnesses
We start with testers for a fixed period .
Lemma 15.
For , Algorithm 3 is a non-adaptive one-sided error -tester for weak -periodicity with complexity .
Proof.
Observe that the algorithm only returns “no” if it finds an implicit -witness. By Lemma 13, a weakly -periodic string contains no implicit -witness; thus, the tester has one-sided error, i.e., it answers “yes” for every weakly -periodic string.
Now, assume that is -far from being weakly -periodic. If the algorithm answers “yes”, then at least one of the following holds:
-
the algorithm samples more than positions in the first stage;
-
the set of sampled positions does not contain an implicit -witness.
The probability of the first event is at most via a Chernoff bound (Fact 33 invoked with ). We only query the input if and thus . Hence, the complexity is as claimed. For the second event, we show that, with constant probability, we find (non-explicit) -witnesses in different equivalence classes. These pairs form an implicit -witness.
We use the construction from Lemma 14, consisting of and, for , sets such that for every . Since we sample with rate , by Lemma 14, we expect to hit each set independently with probability at least . If we hit , then we also sample another position from the same equivalence class in the second stage. This position is in with probability at least due to Lemma 14. Hence, with probability at least , we hit both and .
Finally, the expected number of for which we succeed, i.e., for which we hit both and and thus find a witness pair, is at least , where the middle step follows from Lemma 14. Since each succeeds independently, we can apply a Chernoff bound (Fact 33) with to show that we succeed for less than classes with probability at most . Otherwise, the at least discovered pairs constitute an implicit witness. Finally, using the union bound for the two sources of error, we answer “yes” for an -far instance with probability .
In the approach above, we guarantee that the -witnesses are disjoint by finding them in distinct equivalence classes. Hence, the approach becomes unsuitable for small . For example, if , then we have to find multiple witnesses in one equivalence class.
We propose a different approach for small . In this case, let and assume that , and . We introduce the following factorization scheme that will be used to detect witness pairs: first, we split into equivalence classes , and then we read and factorize each from left to right. Assume that we have already factorized . The next factor is , where is the minimal value such that either or . Hence, each factor is the shortest prefix of the remaining string that has exactly occurrences of minority symbols. Further, we call a factor with exactly occurrences of minority symbols a complete factor. This factorization only serves the proof and is not actually computed.
Lemma 16.
If is -far from being weakly -periodic, then has at least complete factors over all the equivalence classes, assuming that and for some .
Proof.
We first show that has at least factors. If has at most factors, we can obtain a weakly -periodic string with substitutions as follows. We first substitute all minority symbols of each factor with its majority symbol. Since , we have . Hence, substituting the minority symbols requires at most
substitutions. We perform another substitutions to replace each wildcard with the majority symbol of its surrounding factor. Afterwards, each factor is over a unary alphabet. Since there are at most factors, and each of the equivalence classes contributes at least one factor, there are only boundaries between factors. For each boundary, we perform one substitution to make one of the boundary symbols into a wildcard. As a result, the entire string is weakly -periodic. The total number of substitutions is at most . Hence, has at least factors, and thus at least complete factors.
Lemma 17.
Let . For , and , Algorithm 4 is a non-adaptive one-sided error -tester for weak -periodicity with complexity .
Proof.
Since we only answer “no” if we find an implicit -witness, we always answer “yes” for periodic instances due to Lemma 13. Assume now that is -far from being weakly -periodic. Consider a complete factor of . By Lemma 16, there are of those. By Proposition 5 there is a bipartition , of the alphabet such that the symbols of (resp. ) correspond to a set of at least positions of the factor. Crucially, if we pair up any position with a symbol from with any position with a symbol from , we obtain a witness pair. Furthermore, since factors in the same equivalence class do not overlap, a set of witness pairs over the different factors forms an implicit witness. As a result, if the algorithm answers “yes”, then at least one of the following holds:
-
the algorithm samples more than positions;
-
the algorithm misses a witness pair in at least one of the complete factors.
The probability of the first event is at most via a Chernoff bound (Fact 33 invoked with ). We only query the input if , and thus the complexity is as claimed. The probability of not sampling a position corresponding to a symbol in (analogously in ) in a fixed factor is at most . Hence, by the union bound, we fail for at least one factor with probability at most . Combining the two sources of failure by the union bound, we answer “yes” for a far instance with probability at most .
4.2 Testing Weak -Periodicity Through Implicit Witnesses
We now present a tester for weak -periodicity which uses implicit witnesses.
Lemma 18.
Let . For and , Algorithm 5 is a non-adaptive one-sided error -tester for weak -periodicity with complexity .
Proof.
The analysis is similar to that of Algorithm 4, but with a higher sampling rate. By Lemma 13, the algorithm answers “yes” for strings that have a weak period .
Otherwise, i.e., for an input that is -far from being weakly -periodic, the algorithm only answers “yes” if it samples more than indices, or if, for a certain , it does not find a witness pair in at least one complete factor. The first event occurs with probability at most via a Chernoff bound (Fact 33 invoked with ). For a fixed and a fixed complete factor, the second event occurs with probability at most (via the same calculation as in Lemma 17). Therefore, over all possible periods and their sets of positions, the union bound implies that we fail for at least one set with probability at most . If is indeed -far from being weakly -periodic, and thus also from being weakly -periodic for every , then with probability at least we can report a witness for every .
Lemma 19.
For , Algorithm 6 is a non-adaptive one-sided error -tester for weak -periodicity with complexity .
Proof.
We only answer “no” if both of the following events occur: Algorithm 5 returns “no” at least once (which by Lemma 18 implies that has no period ), and we find an implicit -witness for every period . Hence, we never return “no” if the input string is weakly -periodic. It remains to handle strings that are -far from all periods in . We have to show that we accept them with probability at most .
First, by Lemma 18, if is -far from being weakly -periodic, then the probability of all three calls to Algorithm 5 returning “yes” is . The number of queries is , which is bounded by if ; otherwise, and hence the number of queries is .
We next bound the probability of sampling at least indices. This happens with probability at most via a Chernoff bound (Fact 33 with ). We only query the input if the total number of samples is at most . Hence, the complexity is as claimed.
If we enter the second stage, then let be the event that the algorithm does not find an implicit -witness. By the union bound, we answer “yes” due to one of these events with probability at most . Hence, we can conclude the proof by showing that , which implies that we answer “yes” with probability at most . Then, via the union bound, the error probabilities from all sources sum to .
From now on, fix any . We perform the analysis with a lower sampling rate , which can only lower our success probability. We use the construction from Lemma 14, consisting of and, for , sets such that for every . Due to , by Lemma 14, we expect to hit each set (resp. ) independently with probability (resp. ). For any , we hit both and (and thus discover a witness pair) with probability at least . The expected number of different for which we are successful is , where the second step is due to Lemma 14. Finally, applying the Chernoff bound from Fact 33 with , we are successful for at most classes with probability at most .
4.3 Testing Weak Periodicity Through Explicit Witnesses
Lemma 20.
Consider a -wildcard string that is -far from being -periodic, where . There are at least explicit -witnesses.
Proof.
At least of equivalence classes of are not weakly -periodic. Otherwise we could simply overwrite these classes with unary strings using less than substitution. This would result in a weakly -periodic string, which contradicts the fact that is -far. Consequently, classes have at least one explicit -witness each.
We now prove that the number of explicit witnesses exceeds . To do so, we further greedily factorize each equivalence class from left to right into maximal factors of the form for any . We then obtain a string without wildcards by replacing each wildcard in with the symbol of its surrounding factor. This requires substitutions and results in a string that is -far from being weakly -periodic. Furthermore, if contains explicit witnesses, then contains at least explicit witnesses.
Let be the total number of maximal unary factors over all equivalence classes in . Since is -far from being weakly -periodic, we have , or else it could be made into a weakly -periodic string by adding at most wildcards at factor boundaries. We say that a maximal unary factor is short if it is among the shortest maximal unary factors, and long otherwise. We claim that the short factors must contribute at least symbols overall. Otherwise, we can again show that substitutions are sufficient to make periodic: We replace each symbol of a short factor with the symbol populating the closest long factor in its equivalence class (or with if such run is undefined). This effectively merges each short factor with a long one. Every remaining run is either an extension of one of the original long factor, or an equivalence class that consists of a single factor. The total number of long factors in all classes that contain at least two long factors is at most . Hence, we can fix their at most boundaries with wildcards. The total number of substitutions is less than .
Since short runs contribute symbols, there is a short run of length . Consequently, all long factors have length at least . The total length of long runs is therefore at least . On the other hand, it is upper-bounded by , which implies . Hence, the number of explicit -witnesses in is at least , which implies that contains at least explicit -witnesses, concluding the proof.
The following two lemmas follow from Lemma 20 in a straightforward manner.
Lemma 21.
Algorithm 7 is a non-adaptive -tester for weak -periodicity with complexity .
Proof.
Since weakly -periodic strings do not contain explicit -witnesses (see Lemma 13), they are accepted by Algorithm 7. Consider now a string that is -far from being weakly -periodic. By Lemma 20, the probability that a given pair sampled by the algorithm is an explicit witness is at least . Hence, the probability that the algorithm does not find an explicit witness is at most .
Lemma 22.
Algorithm 8 is a non-adaptive -tester for weak -periodicity with complexity .
Proof.
First, consider a weakly -periodic string for some . It does not contain an explicit -witness, and hence is accepted by Algorithm 8. Now, consider a string that is -far from being weakly -periodic for all . Fix one , . By Lemma 20, has at least explicit -witnesses. Furthermore, since there are at most explicit -witnesses sharing the same index, there are at least explicit witnesses that are mutually disjoint. Let be the number of these explicit -witnesses sampled by the algorithm. is a sum of independent indicator random variables, and therefore we have
Hence, by the union bound, the probability that the algorithm misses an explicit witness for at least one such that is -far is at most . Furthermore, by Fact 33 invoked with , the probability that the algorithm samples more than positions is at most . By the union bound, the probability that the algorithm answers “no” is at least .
4.4 Testers for Weak Periodicity: Wrap-up
Corollary 23.
For , there is a non-adaptive -tester for weak -periodicity with complexity .
Proof.
We first show that there is a tester with complexity . If then the claimed number of queries exceeds , and we simply query the whole string. Otherwise, at least one of or holds, and hence we can use one of Lemma 15 and Lemma 17 with . The final claim follows by combining with Lemma 21.
Corollary 24.
For , there is a non-adaptive -tester for weak -periodicity with complexity .
Proof.
5 Lower Bounds for Weak Periodicity Testing
The proof of the following proposition is deferred to Appendix B.
Proposition 25.
Let , , and . For , define the set . If we choose uniformly at random, then for all , .
Proposition 26.
Let be a string with at most wildcards, a subset of its positions, and satisfy for . If contains neither an implicit nor an explicit -witness, then the symbols , , can be chosen so that is weakly -periodic.
Proof.
Consider an equivalence class of . If contains only undefined symbols, we fill it with an arbitrary symbol of and exclude it from further consideration. Otherwise, we divide from left to right into maximal factors such that, for each such factor, all its symbols are either equal to a fixed symbol of or undecided. For two neighboring factors and , we cannot have both the symbols adjacent to the boundary defined (i.e., we cannot have both ). Otherwise, the maximality property implies that and must be different, and hence we have an explicit -witness in . Let be the number of factors in . We have , or otherwise we can form an implicit -witness in by taking pairs of positions of defined symbols in neighboring factors. Now, we explain how to complete . We place a wildcard in one of the boundary positions of each pair of neighboring factors, which requires at most wildcards. Then, we fill each factor with the single symbol from occurring in it. The resulting string is weakly -periodic and contains at most wildcards.
Next, we define and analyze an auxiliary distribution on strings in . We start by drawing a potential period uniformly at random. The classes , for , are chosen to be unary: for all , . The remaining classes are constructed as follows. For each , we consider a partition , where each is of length or , contains only zeros, and contains only ones. is constructed using a two-state Markov chain as follows: and, for every position , we assign with probability , and otherwise. is defined in the same way as , but starting with a .
Lemma 27.
A random string of length drawn according to satisfies each of the following for integers and with and a constant :
-
is -far from a weakly -periodic string for all with with probability and -far from a weakly -periodic string with probability at least ;
-
has less than explicit -witnesses with probability at least .
Proof.
Claim 28.
is -far from a weakly -periodic string for any with .
Proof.
For every we have , because , meaning that is in a different class modulo (either in a different class among the first classes or in a binary class). This implies that in order to obtain a weak -periodic string from one needs to change at least half of the symbols of the classes , , or symbols in total. Hence is -far from every period in .
Claim 29.
With probability at least , has less than explicit -witnesses.
Proof.
Let be the number of pairs such that and . Let , which is an upper bound on the total number of explicit -witnesses in . We have , where takes care of the ceilings and floors, and the transition between and . Hence, we have , where . Then, by Fact 33, with probability . As we have , the claim follows.
Claim 30.
With probability at least , is -far from a weakly -periodic string.
Proof.
To make weakly -periodic, we must ensure that each equivalence class can be partitioned into factors of identical symbols separated by wildcards, where . We show that we cannot achieve this by changing at most symbols with good probability. Since and , there are at least classes such that . Among those, there are at least classes where we can change at most symbols. We consider one such class with , which is possible since . It contains at least zeros and at least ones. Hence, we must change at least symbols to obtain a unary string.
Theorem 31.
Every non-adaptive one-sided -tester for weak -periodicity has complexity , assuming and .
Proof.
We use Yao’s principle adapted for one-sided algorithms: to prove that a one-sided tester requires at least queries, we show a distribution on -wildcard strings for which every deterministic tester that performs queries either answers “no” on a weakly -periodic string with non-zero probability, or answers “yes” on an -far string with probability greater than (when the string is drawn via ). The distribution is defined as follows: with probability , a string is drawn according to , and with probability we draw a weakly -periodic string uniformly at random.
By Proposition 26, if the set of queried positions does not form either explicit or implicit -witnesses for some , the input can be completed as a weakly -periodic string. Thus, assuming that the tester does not return “no” on weakly -periodic strings with positive probability, the tester must return “yes” if the set of queried symbols contains neither explicit nor implicit -witnesses for some .
Henceforth, we assume that the input string drawn according to is -far from a weakly -periodic string, which by Lemma 27 happens with probability at least . It suffices to upper bound the probability that a deterministic algorithm hits either an implicit or an explicit -witness for all on such string. By Lemma 27, this probability is upper-bounded by the probability that an algorithm hits an implicit or an explicit -witness.
Fix an arbitrary set of size . If , then by Lemma 27 the probability that and hence the probability that the algorithm hits an implicit -witness is at most (regardless of the actual string).
Now note that the space of explicit -witnesses can be defined as , since for each explicit -witness we have . Consequently, if , then by Lemma 27, the expected number of explicit -witnesses hit by a deterministic algorithm is less than , which implies that a deterministic algorithm misses an explicit -witness with probability at least by Markov’s inequality (Fact 32).
Finally, let . By Proposition 25, we have . If , then . Furthermore, by Lemma 27, the expected number of explicit -witnesses hit by is less than conditional on . Hence, the probability that that a deterministic algorithm misses an explicit -witness conditional on is at least by Markov’s inequality (Fact 32). By the union bound, the probability that an algorithm misses all explicit witnesses in this case is at least .
Thus, if any of these conditions of is satisfied, the algorithm fails with probability at least , a contradiction. The statement follows.
References
- [1] Christine Awofeso, Ben Bals, Oded Lachish, and Solon P. Pissis. Testing quasiperiodicity. In Proceedings of the International Symposium on String Processing and Information Retrieval (SPIRE 2026), pages 1–9, 2026. doi:10.1007/978-3-032-05228-5_1.
- [2] Jean Berstel and Luc Boasson. Partial words and a theorem of Fine and Wilf. Theoretical Computer Science, 218(1):135–141, 1999. doi:10.1016/S0304-3975(98)00255-2.
- [3] F. Blanchet-Sadri. Periodicity on partial words. Computers & Mathematics with Applications, 47(1):71–82, 2004. doi:10.1016/S0898-1221(04)90006-5.
- [4] F. Blanchet-Sadri, Deepak Bal, and Gautam Sisodia. Graph connectivity, partial words, and a theorem of Fine and Wilf. Information and Computation, 206(5):676–693, 2008. Special Issue: The 17th International Conference on Concurrency Theory (CONCUR 2006). doi:10.1016/j.ic.2007.11.007.
- [5] F. Blanchet-Sadri, Travis Mandel, and Gautam Sisodia. Periods in partial words: An algorithm. Journal of Discrete Algorithms, 16:113–128, 2012. doi:10.1016/j.jda.2012.04.001.
- [6] Francine Blanchet-Sadri, Taktin Oey, and Timothy D. Rankin. Fine and Wilf’s theorem for partial words with arbitrarily many weak periods. Int. J. Found. Comput. Sci., 21(5):705–722, 2010. doi:10.1142/S0129054110007519.
- [7] Francine Blanchet-Sadri and Nathan D. Wetzler. Partial words and the critical factorization theorem revisited. Theor. Comput. Sci., 385(1-3):179–192, 2007. doi:10.1016/J.TCS.2007.06.012.
- [8] Timothy M. Chan, Shay Golan, Tomasz Kociumaka, Tsvi Kopelowitz, and Ely Porat. Approximating text-to-pattern Hamming distances. In Proceedings of the 52nd Annual ACM SIGACT Symposium on Theory of Computing (STOC 2020), pages 643–656. ACM, 2020. doi:10.1145/3357713.3384266.
- [9] Funda Ergun, S. Muthukrishnan, and Cenk Sahinalp. Periodicity testing with sublinear samples and space. ACM Trans. Algorithms, 6(2), April 2010. doi:10.1145/1721837.1721859.
- [10] Funda Ergün, S. Muthukrishnan, and Süleyman Cenk Sahinalp. Sublinear methods for detecting periodic trends in data streams. In LATIN 2004, volume 2976 of LNCS, pages 16–28, 2004. doi:10.1007/978-3-540-24698-5_6.
- [11] Nathan J. Fine and Herbert S. Wilf. Uniqueness theorems for periodic functions. Proceedings of the American Mathematical Society, 16(1):109–114, 1965. doi:10.2307/2034009.
- [12] Svante Janson, Tomasz Łuczak, and Andrzej Ruciński. Random Graphs, chapter Exponentially Small Probabilities, pages 25–51. Wiley-Interscience, 2000. doi:10.1002/9781118032718.ch2.
- [13] Ce Jin and Tomasz Kociumaka. Near-optimal property testers for pattern matching. In 66th IEEE Annual Symposium on Foundations of Computer Science (FOCS 2025), pages 1639–1647. IEEE Computer Society, 2025. doi:10.1109/FOCS63196.2025.00086.
- [14] Tomasz Kociumaka, Jakub Radoszewski, Wojciech Rytter, and Tomasz Waleń. A periodicity lemma for partial words. Inf. Comput., 283(C), February 2022. doi:10.1016/j.ic.2020.104677.
- [15] Oded Lachish and Ilan Newman. Testing periodicity. Algorithmica, 60(2):401–420, 2011. doi:10.1007/s00453-009-9351-y.
- [16] Michael Mitzenmacher and Eli Upfal. Probability and Computing: Randomized Algorithms and Probabilistic Analysis. Cambridge University Press, 2nd edition, 2017.
- [17] J. Barkley Rosser and Lowell Schoenfeld. Approximate formulas for some functions of prime numbers. Illinois Journal of Mathematics, 6(1), March 1962. doi:10.1215/ijm/1255631807.
- [18] A. M. Shur and Yu. V. Gamzova. Partial words and the interaction property of periods. Izvestiya: Mathematics, 68(2):405–428, 2004. doi:10.1070/IM2004v068n02ABEH000480.
- [19] Arseny M. Shur and Yulia V. Konovalova. On the periods of partial words. In Mathematical Foundations of Computer Science 2001, pages 657–665, 2001. doi:10.1007/3-540-44683-4_57.
- [20] W.F. Smyth and Shu Wang. A new approach to the periodicity lemma on strings with holes. Theoretical Computer Science, 410(43):4295–4302, 2009. doi:10.1016/j.tcs.2009.07.010.
Appendix A Probability Inequalities Used in This Work
Fact 32 (Markov’s inequality).
Let be a nonnegative random variable and .
Fact 33 (Multiplicative Chernoff bounds, see e.g. [16]).
Let be independent random variables taking values in and let , . Then, for any ,
and for any
Fact 34 (The extended Janson inequality [12, Theorem 2.14 and Remark 2.15]).
Let be a finite universal set and let be a random subset of given by , these events are mutually independent over .
Let be subsets of , where a finite index set, and be the event . Let be the indicator random variable for and be the number of .
For we write if and . Define and , then .222The original claim in [12] states instead of ; for , this can be strengthened to by observing that
Appendix B Omitted Proofs
Lemma 10. [Restated, see original statement.]
For and , Algorithm 2 is a one-sided -property tester for strong -periodicity with complexity .
If , for and , Algorithm 2 is a one-sided -property tester for strong -periodicity with complexity .
Proof.
Assume that the given string is strongly -periodic with . Let , then . By the definition of witnesses, every -witness is also a -witness. By Observation 7, the tester does not find a -witness and returns “yes”. We have shown that the tester accepts all strings that have a strong -period for some .
Now, assume is -far from being strongly -periodic. If the algorithm answers “yes”, then at least one of the following holds:
-
the algorithm samples positions (henceforth called event );
-
does not contain a -witness for some (henceforth called event ).
We will show that and . Then, by the union bound, the probability that at least one of the events occurs (and hence the probability to answer “yes”) is at most . The probability of is at most via a Chernoff bound (Fact 33 invoked with ). We only query the input if and hence the complexity is as claimed.
For estimating the probability of , let be the event that the algorithm does not find a -witness. Then, by the union bound, . Hence, we can conclude the proof by showing that , which implies .
Let be the set of -witnesses. Lemma 8 implies that . (One gets this bound by observing that, in both cases for , the product of and the corresponding bound from Lemma 8 equals ). For a fixed -witness, the probability of sampling both of its positions is . Hence, the expected number of sampled -witnesses is at least .
By the extended Janson inequality (Fact 34), the probability of not sampling any -witness is at most . Here, is a correlation term that accounts for the fact that pairs are not sampled independently. To explain , we introduce the following notation. For two potential witnesses and , we write if and only if , i.e., if they share exactly one position. Note that the probability of sampling both and is . Also, the three distinct positions of and are in the same equivalence class. Now we can define and provide an upper bound using
For our range of , we have , which readily implies . Finally, if then . Otherwise, . Hence, for , which concludes the proof.
Proposition 35.
For every integer , we have .
Proof.
The number of primes in is at least for . For , it is at least , see Theorem 2 and Corollary 1 of [17].
Proposition 25. [Restated, see original statement.]
Let , , and . For , define the set . If we choose uniformly at random, then for all , .
Proof.
By Proposition 35, contains at least primes. For every , we know that divides . By the prime factorization theorem, has at most distinct prime factors, and thus each pair participates in the set for at most different choices of . Hence, . Therefore, . The claim follows by Markov’s inequality (Fact 32).
