String-searching algorithm ========================== A string-searching algorithm, sometimes called string-matching algorithm, is an algorithm that searches a body of text for portions that match by pattern. A basic example of string searching is when the pattern and the searched text are arrays of elements of an alphabet (finite set) Σ. Σ may be a human language alphabet, for example, the letters A through Z and other applications may use a binary alphabet (Σ = {0,1}) or a DNA alphabet (Σ = {A,C,G,T}) in bioinformatics. In practice, the method of feasible string-search algorithm may be affected by the string encoding. In particular, if a variable-width encoding is in use, then it may be slower to find the Nth character, perhaps requiring time proportional to N. This may significantly slow some search algorithms. One of many possible solutions is to search for the sequence of code units instead, but doing so may produce false matches unless the encoding is specifically designed to avoid it. A string-searching algorithm, sometimes called string-matching algorithm, is an algorithm that searches a body of text for portions that match by pattern. A basic example of string searching is when the pattern and the searched text are arrays of elements of an alphabet (finite set) Σ. Σ may be a human language alphabet, for example, the letters A through Z and other applications may use a binary alphabet (Σ = {0,1}) or a DNA alphabet (Σ = {A,C,G,T}) in bioinformatics. In practice, the method of feasible string-search algorithm may be affected by the string encoding. In particular, if a variable-width encoding is in use, then it may be slower to find the Nth character, perhaps requiring time proportional to N. This may significantly slow some search algorithms. One of many possible solutions is to search for the sequence of code units instead, but doing so may produce false matches unless the encoding is specifically designed to avoid it. === Naive string search=== A simple and inefficient way to see where one string occurs inside another is to check at each index, one by one. First, we see if there is a copy of the needle starting at the first character of the haystack; if not, we look to see if there's a copy of the needle starting at the second character of the haystack, and so forth. In the normal case, we only have to look at one or two characters for each wrong position to see that it is a wrong position, so in the average case, this takes O(n + m) steps, where n is the length of the haystack and m is the length of the needle; but in the worst case, searching for a string like "aaaab" in a string like "aaaaaaaaab", it takes O(nm) ===Stubs=== Knuth–Morris–Pratt computes a DFA that recognizes inputs with the string to search for as a suffix, Boyer–Moore starts searching from the end of the needle, so it can usually jump ahead a whole needle-length at each step. Baeza–Yates keeps track of whether the previous j characters were a prefix of the search string, and is therefore adaptable to fuzzy string searching. The bitap algorithm is an application of Baeza–Yates' approach. In the following compilation, m is the length of the pattern, n the length of the searchable text, and k = |Σ| is the size of the alphabet. The Boyer–Moore string-search algorithm has been the standard benchmark for the practical string-search literature. ===Real-time string matching=== In real-time string matching, one requires the matcher to output a response after reading each character of the text, that indicates whether this is the last character of a match. The response has to be given within constant time. The requirement regarding preprocessing vary: O(m) preprocessing may be allowed after the pattern is read (but before the reading of the text), or a stricter requirement may be posed according to which the matcher has to also pause for at most a constant time after reading any character of the pattern (including the last). For the more lenient version, if one does not mind that the preprocessing time and memory requirement dependend on the size of the alphabet, a real-time solution is provided by automaton matching. Zvi Galil developed a method to turn certain algorithms into real-time algorithms, and applied it to produce a variant of the KMP matcher that runs in real time under the strict requirement. ==String searching with don't cares== In this version of the string searching problem, there is a special symbol, ø (read: don't care), which can match any other symbol (including another ø). Don't care symbols can appear either in the pattern or in the text. In 2002, an algorithm for this problem that runs in O(n\log m) time has been given by Richard Cole and Ramesh Hariharan, improving on a solution from 1973 by Fischer and Paterson that has complexity O(n\log m\log k) , where k is the size of the alphabet. Another algorithm, claimed simpler, has been proposed by Clifford and Clifford. ==See also== Sequence alignment Graph matching Pattern matching Compressed pattern matching Matching wildcards Approximate string matching Full-text search Two-dimensional pattern matching ==References== ==Further reading== R. S. Boyer and J. S. Moore, A fast string searching algorithm, Carom. ACM 20, (10), 262–272(1977). Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Third Edition. MIT Press and McGraw-Hill, 2009. . Chapter 32: String Matching, pp. 985–1013. ==External links== Huge list of pattern matching links Last updated: 12/27/2008 20:18:38 Large (maintained) list of string-matching algorithms NIST list of string-matching algorithms StringSearch – high-performance pattern matching algorithms in Java – Implementations of many String-Matching-Algorithms in Java (BNDM, Boyer-Moore-Horspool, Boyer-Moore-Horspool-Raita, Shift-Or) StringsAndChars – Implementations of many String-Matching-Algorithms (for single and multiple patterns) in Java Exact String Matching Algorithms — Animation in Java, Detailed description and C implementation of many algorithms. (PDF) Improved Single and Multiple Approximate String Matching Kalign2: high-performance multiple alignment of protein and nucleotide sequences allowing external features NyoTengu – high-performance pattern matching algorithm in C – Implementations of Vector and Scalar String-Matching-Algorithms in C Nathaniel K. Brown, et al.: "KeBaB: k-mer based breaking for finding long MEMs", arXiv:2502.20338v3 (09 Jun 2025). REFERENCES ---------- BIBLIOGRAPHY (BibTeX) -------------------- @article{ref0, title = {String-searching algorithm}, url = {https://encyclopegia.org/a/string-searching-algorithm} } BIBLIOGRAPHY (RIS) ------------------- TY - JOUR TI - String-searching algorithm UR - https://encyclopegia.org/a/string-searching-algorithm ER - CITE THIS ARTICLE ---------------- Cite this article: String-searching algorithm Derived from: https://en.wikipedia.org/w/index.php?oldid=1368389768 Licence: CC BY-SA 4.0 Snapshot: enwiki-001368389768 Permalink: /a/string-searching-algorithm