Similarity searches commonly known as approximate string matching allow for some mismatches between the text and the pattern. Similarity searches are widely used in computational biology, search engines, data mining, signal processing, digital dictionaries, and many other applications where exact and similar patterns are to be searched over a given text. Most of the algorithms developed prior to the 1990’s such as Morris and Pratt , Aho and Corasick , Knuth et al. , Boyer and Moore , Horspool , and Karp and Rabin , were designed to search for exact pattern matches in the text. However, with some modifications, they can also be used to find approximate matches.
The term “distance” is often used when comparing two strings for similarity. A lesser distance is expected for a greater similarity. The Hamming distance  approximates the similarity between two strings of equal length by measuring the number of character mismatches at corresponding locations. Let R and S be two non-empty equal-length strings of size M such that and . Then, the Hamming distance between R and S is given by ham(R, S) = number of locations where , . Clearly, if R and S are identical, then ham(R, S) = 0. For a given pattern P modern applications may require database to be searched for exact or approximate matches of P. In the literature, this problem is sometimes referred as “string matching with k mismatches” which can be stated as follows: Given the text of size N, and the pattern of size M defined over a finite set of text character called alphabet λ. Let hdi be the Hamming distance such that , where, . Then, for a given integer k such that , report all locations iin T where .
To solve the “k-mismatch” problem, Landau and Vishkin  proposed a suffix-tree-based algorithm. They used a suffix tree to preprocess the text and the pattern in O(N + M) time, and then report k mismatches in O(kN) time. However, the algorithm requires O(k(M + N)) space, which is a concern when N becomes large. Galil and Giancarlo  presented an algorithm that uses O(kN) time and O(M) space to solve the same problem. Amir et al.  developed an algorithm to identify all such locations in time. As “k” increases the performance of these algorithms deteriorates, and approaches O(MN) as “k” approaches M. For k=M, the problem becomes independent of k and reduces to “string matching with mismatches”, which can be stated as follows: Given the text , and the pattern , for every i such that , output the Hamming distance hdi such that . Abrahamson  applied a technique known as the Boolean convolution of the pattern and the text to solve the problem in time and O(N) space. Using a linked list, Yates and Perleberg  presented O(N + Nfmax) time and O(2M + σ) space algorithm, where fmax is the frequency of the most commonly occurring character in the pattern. Many of these algorithms are covered in Crochemore et al. . For a detailed survey on approximate string matching refer to Navarro , Boytsov . Most of the algorithms developed in the past use data-structures and methods outlined above to create indexes over the text or the pattern to accelerate the search process. However, their costly maintenance has always been a cause of concern.
2. Assumptions and Notations
We use the following assumptions and notations. “λ” represents a finite non-empty ordered set of characters called an alphabet, such that is the size of the alphabet. “λi” is the ith character in λ such that . String S is a finite sequence of characters defined over alphabet λ. S[i] or Si represents the ith character of string S, where, “i” is refers to as the shift, location, or index in S. Both T and P represent non-empty text and pattern strings defined over the alphabet λ. N and M represent sizes of the text and the pattern respectively such that M ≤ N. We use the phrase “Number of matches of P in T at shift t”, which refers to the number of character matches when pattern P is aligned with shift t in T.
3. The Model
Traditional, pattern matching algorithms attempts to align P from the first character of T which may result in losing some valuable information regarding the character matches. Let’s consider an example: suppose pattern P = ABCDEF, and text T = CDEFABCD. Assuming 0 being the initial index of the strings, a four character match is found when P is aligned at the index −2 in T. All traditional algorithms would lose this information as attempts are made to align P from index 0 in T. In other words, traditional algorithms consider the indexes in the range , where i represent the text index. However, as we have seen, considering i’s in the range may provide additional information, particularly when the pattern is considerably large, and the character matches exist at opposite ends of the pattern or text. The lemma and proof given below are already discussed in our previous work . However, a brief discussion is provided below for a prompt reference.
Let T and P be non-empty text and pattern strings defined over an alphabet λ such that: and . Corresponding to every index j in P, we define a set Rj such that . Further, let S represent a set such that .Then:
1) Every integer represents an index in T where an exact match of P is found when P is aligned at s in T.
2) The cardinality |S| represents the number of occurrences of P in T. If |S| =0 then P is not present in T.
Proof: Suppose P is found in T when P is aligned at index s in T. Then, we have to show that . If P appears in T at shift (index)s that means all M characters of pattern can be successfully matched with . Hence, in P such that , we have . Now, from the definition of Rj, we get ⇒ , ⇒ . Further, since all represent an exact match of P when P is aligned at index s in T ⇒ |S| = Number of occurrences of P in T.
Example 1: Let T = GCABABABCBA be a text array and P = ABAB be a pattern array of size 4. For the given text we have i such that . For each shift j ( ) in P we create a set Rj such that . Which gives: , , , and . Hence, ⇒ , which shows P occurs in T twice at locations 2 and 4.
Corollary 1: Given the M sets Rj defined as in the lemma given above. Let fs be the frequency of the occurrence of an integer “s” in all sets. Then, fs represents the number of character matches when P is aligned with shift s in T.
Proof: We have already proved that any represents exactly M character matches of at shift s in T ⇒ each represents a single character match pj of P ⇒ the frequency of integer “s” = fs = Number of character matches of P at shift s in T. Please note: for an exact match of P at shift s in T, “s” must be present in all M sets, in that case: , i.e. fs= M. Also, M - fs represents the Hamming distance, i.e. the number of character mismatches when pattern P is aligned with shift s in T.
Example 2: Consider example 1, the integer 6 appears in three sets: R0, R1 and R3. Hence, the frequency of the integer 6 is equal to f6 = 3. Which shows three character matches of P when P is aligned with shift 6 in T. Similarly, reveal two character matches of P when P is aligned with locations 0 and 80 in T.
4. Model Implementation
We present another example to see how the model described above can be implemented successfully. Consider the pattern array P = FCTHZCTZCF, and the text array T = SKRFCTHZCTZCFTYCTZGHTTCTHZTHZFCTHZCTZCFT. The first column of Table 1 below summarizes all unique characters of the pattern. The second and third columns of the table present the shifts of the corresponding pattern characters in the text and in the pattern respectively. Each row of the last column represents set Rj as defined in the lemma.
As noted above, the frequency of occurrence fs of an integer “s” in all set represents the number of character matches of P at shift sin T. Therefore, we simply need a mechanism for counting the number of occurrences of individual “s” in all sets in Table 1. This can be done using an array of integers “hit” of size N with all cells initialized to 0. Then, for each the count at hit[s] is incremented by one. In other words, each induces a hit at “hit[s]”. Figure 1 shows the resulting array. The first row of Figure 1 represents array indexes, and middle row indicates the hit count at the corresponding index. Note that locations 3 and 29 have been hit 10 times, which indicate 10 character matches of P and T at alignment locations 3 and 29 in T. Moreover, location 21
Table 1. Hit index.
Figure 1. The hit array.
Figure 2. The re-indexed hit array.
has been hit 5 times, location 14 has been hit 4 times, representing 5 and 4 character matches of P at alignment locations 21 and 14 respectively.
The method described above has two shortcomings. First, we need an array of size N, which is undesirable for large values of N. To resolve this, the hit can be assumed as cyclic that allow us to reuse the previously used array cells. Second, as we have seen in previous examples that we might get negative values of s for which the array cell does not exist. For example, s = −1 in the two sets R5 and R6 in Table 1, suggesting that we can obtain two character matches if P is aligned at location −1 in T. Such hits are ignored in Figure 1 because as we cannot record hits at negative array locations. This issue can be resolved by assuming the initial index of the text file to be ≥M − 1 rather than 0. This will ensure that s ≥ 0 for all . Figure 2 shows the re-indexed version of the array, where, each text character location is hyped by M. Therefore, index M = 10 in the re-indexed array corresponds to location 0 of the actual text array, index 9 corresponds to location −1, and so on. The benefit is straightforward; with the re-indexed array, we can say that 2 hits are found if the pattern is aligned at location 9, which corresponds to location -1 in the actual text array.
Highly practical solutions can be drawn based on the model we have presented in this paper. The novel approach we have followed may become an alternative to existing solutions.
We thank anonymous reviewers for their comments and suggestions.
 Boytsov, L. (2011) Indexing Methods for Approximate Dictionary Searching: Comparative Analysis. ACM Journal of Experimental Algorithmics, 16, Article No. 1.1.
 Vinod-Prasad, P. (2016) A Novel Algorithm for String Matching with Mismatches. 5th International Conference of Pattern Recognitions Applications and Methods, Rome, February 2016, 638-644.