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
- ^ Kurtz, Stefan; Phillippy, Adam; Delcher, Arthur L; Smoot, Michael; Shumway, Martin; Antonescu, Corina; Salzberg, Steven L (2004). "Versatile and open software for comparing large genomes" . Genome Biology . 5 (2): R12. doi : 10.1186/gb-2004-5-2-r12 . ISSN 1465-6906 . PMC 395750 . PMID 14759262 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Fan, H.; Yao, N.; Ma, H. (December 2009). "Fast Variants of the Backward-Oracle-Marching Algorithm" (PDF) . 2009 Fourth International Conference on Internet Computing for Science and Engineering . pp. 56– 59. doi : 10.1109/ICICSE.2009.53 . ISBN 978-1-4244-6754-9 . S2CID 6073627 . Archived from the original on 2022-05-10 . Retrieved 2019-11-22 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1574377200.0
- ^ Melichar, Borivoj, Jan Holub, and J. Polcar. Text Searching Algorithms. Volume I: Forward String Matching. Vol. 1. 2 vols., 2005. http://stringology.org/athens/TextSearchingAlgorithms/ Archived 2016-03-04 at the Wayback Machine . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Litwin, Witold; Mokadem, Riad; Rigaux, Philippe; Schwarz, Thomas (2007), Fast nGram-Based String Search Over Data Encoded Using Algebraic Signatures (PDF) , International Conference on Very Large Data Bases Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Gonzalo Navarro; Mathieu Raffinot (2008), Flexible Pattern Matching Strings: Practical On-Line Search Algorithms for Texts and Biological Sequences , Cambridge University Press, ISBN 978-0-521-03993-2 Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Galil, Zvi (1981). "String matching in real time". Journal of the ACM . 28 (1): 134– 149. doi : 10.1145/322234.322244 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Cole, Richard; Hariharan, Ramesh (2002). "Verifying candidate matches in sparse and wildcard matching". Proceedings of the thiry-fourth annual ACM symposium on Theory of computing . pp. 592– 601. Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Clifford, Peter; Clifford, Raphaël (January 2007). "Simple deterministic wildcard matching". Information Processing Letters . 101 (2): 53– 54. doi : 10.1016/j.ipl.2006.08.002 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Khan, Zia; Bloom, Joshua S.; Kruglyak, Leonid; Singh, Mona (2009-07-01). "A practical algorithm for finding maximal exact matches in large sequence datasets using sparse suffix arrays" . Bioinformatics . 25 (13): 1609– 1616. doi : 10.1093/bioinformatics/btp275 . PMC 2732316 . PMID 19389736 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Asymptotic times are expressed using O, Ω, and Θ notation . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Crochemore, Maxime; Perrin, Dominique (1 July 1991). "Two-way string-matching" (PDF) . Journal of the ACM . 38 (3): 650– 674. doi : 10.1145/116825.116845 . S2CID 15055316 . Archived (PDF) from the original on 24 November 2021 . Retrieved 5 April 2019 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ "glibc/string/str-two-way.h" . Archived from the original on 2020-09-20 . Retrieved 2022-03-22 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1647903600.0
- ^ "musl/src/string/memmem.c" . Archived from the original on 1 October 2020 . Retrieved 23 November 2019 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Used to implement the memmem and strstr search functions in the glibc [ 4 ] and musl [ 5 ] C standard libraries . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Navarro, Gonzalo; Raffinot, Mathieu (1998). "A bit-parallel approach to suffix automata: Fast extended string matching" (PDF) . Combinatorial Pattern Matching . Lecture Notes in Computer Science. Vol. 1448. Springer Berlin Heidelberg. pp. 14– 33. doi : 10.1007/bfb0030778 . ISBN 978-3-540-64739-3 . Archived (PDF) from the original on 2019-01-05 . Retrieved 2019-11-22 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1574377200.0
- ^ Can be extended to handle approximate string matching and (potentially-infinite) sets of patterns represented as regular languages . [ citation needed ] Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Hume; Sunday (1991). "Fast String Searching". Software: Practice and Experience . 21 (11): 1221– 1248. doi : 10.1002/spe.4380211105 . S2CID 5902579 . Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- ^ Commentz-Walter, Beate (1979). A String Matching Algorithm Fast on the Average (PDF) . International Colloquium on Automata, Languages and Programming . LNCS . Vol. 71. Graz, Austria: Springer. pp. 118– 132. doi : 10.1007/3-540-09510-1_10 . ISBN 3-540-09510-1 . Archived from the original (PDF) on 2017-10-10. Wikipedia revision 1368389768, retrieved 2026-11-21 · 1795215600.0
- Crochemore, Maxime. Two-way string-matching . 1 July 1991. · 1786143600.0
- glibc/string/str-two-way.h . retrieved 2022-03-22 · 1647903600.0
- musl/src/string/memmem.c . retrieved 23 November 2019 · 1786143600.0
- Navarro, Gonzalo. Combinatorial Pattern Matching . · 1786143600.0
- Fan, H. 2009 Fourth International Conference on Internet Computing for Science and Engineering . · 1786143600.0
- Θ(m) |- ! Set-BOM | Backward Oracle Matching | | | |} ==== Algorithms using an infinite number of patterns ==== Naturally, the patterns can not be enumerated finitely in this case. They are represen . · 1786143600.0
- citation |last1=Litwin |first1=Witold |last2=Mokadem |first2=Riad |last3=Rigaux |first3=Philippe |last4=Schwarz |first4=Thomas |url=https://www.vldb.org/conf/2007/papers/research/p207-litwin.pdf |ti . · 1786143600.0
- Commentz-Walter, Beate. A String Matching Algorithm Fast on the Average . · 1786143600.0
Cite this
"String-searching algorithm". 1786211907. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1368389768