Short answer: Ukkonen’s algorithm builds a suffix tree, the compacted trie of all suffixes, online in O(n) time for a fixed alphabet. It processes the string left to right and keeps an active point, the place where the next character must be inserted for every suffix that has not…
Category: Strings
How the Z-algorithm boxes previous prefix matches
Short answer: The Z-algorithm computes, for every index i of a string s, the length Z[i] of the longest substring starting at i that matches a prefix of s. It does this in O(n) time by reusing a window [left, right] that is the rightmost prefix match found so…
How the longest common subsequence fills a grid
Short answer: The longest common subsequence of two strings is the longest sequence that appears in both, in order, not necessarily contiguously. The cell L[i][j] equals L[i−1][j−1] + 1 when the i-th and j-th characters match, and otherwise the maximum of L[i−1][j] and L[i][j−1]. The table is O(nm) time.…
How the Burrows-Wheeler transform sorts rotations
Short answer: The Burrows-Wheeler transform sorts every rotation of a string and emits the last character of each rotation, plus the index of the original string in that sorted list. The output is a permutation that tends to group identical characters, which is why it sits in front of…
How Smith-Waterman aligns two sequences locally
Short answer: Smith-Waterman finds the best local alignment: the highest-scoring pair of substrings under a substitution matrix and a gap penalty. The recurrence is Needleman-Wunsch with one extra choice. A cell may be 0, which means “start a new alignment here.” The answer is the maximum cell anywhere in…
How Rabin-Karp rolls a hash across the text
Short answer: Rabin-Karp hashes the pattern and every window of the text. A polynomial rolling hash updates the next window in constant time: subtract the outgoing character, multiply by the radix, and add the incoming character, all modulo a prime. Equal hashes are candidates and must be verified character…
How prefix doubling builds a suffix array
Short answer: A suffix array is the starting positions of all suffixes of a string, sorted in lexicographic order. Prefix doubling sorts those suffixes by the first character, then by the first 2, 4, 8, … characters, using the ranks from the previous round as a pair key. Each…
How Needleman-Wunsch aligns two sequences globally
Short answer: Needleman-Wunsch computes a global alignment of two sequences by the same prefix grid as edit distance, with a substitution score and a gap penalty in place of unit costs. The corner cell is the optimum over alignments of the entire strings, including gaps at the ends. Affine…
How Manacher mirrors palindrome radii
Short answer: Manacher’s algorithm finds every palindromic substring’s radius, and in particular the longest one, in O(n) time. It transforms the string so odd and even lengths share one code path, then fills a radius array. A window around the rightmost palindrome found so far mirrors radii across its…
How Levenshtein distance fills an edit grid
Short answer: Levenshtein distance is the minimum number of single-character insertions, deletions, and substitutions that turn one string into the other. The DP cell d[i][j] is that distance between the first i characters of the source and the first j characters of the target. It is the minimum of…
How Knuth-Morris-Pratt skips with the prefix table
Short answer: The Knuth-Morris-Pratt algorithm scans a text for a pattern in O(n + m) time. It builds a prefix table π, where π[i] is the longest proper prefix of the pattern that is also a suffix of the pattern ending at i. When a mismatch occurs after i…
How Kasai builds the LCP array from a suffix array
Short answer: Kasai’s algorithm builds the LCP array of a string from its suffix array in O(n) time. LCP[i] is the length of the longest common prefix of the suffixes that are adjacent in sorted order, SA[i−1] and SA[i]. The algorithm walks the suffixes in text order and uses…
How Boyer-Moore skips with the bad-character rule
Short answer: Boyer-Moore aligns the pattern with the text and compares from the right end of the pattern. The bad-character rule shifts so that the mismatched text character lines up with its rightmost occurrence in the pattern, or shifts past the pattern if the character does not occur. The…
How bitap matches a pattern with word shifts
Short answer: Bitap, or the Shift-Or algorithm, matches a pattern by keeping a bitset of the pattern prefixes that match the text so far. Each text character shifts the set and masks it with the character’s precomputed bit mask. A match is the bit at the pattern’s last position.…
How Aho-Corasick matches many patterns at once
Short answer: Aho-Corasick builds a trie of every pattern and adds a failure link from each node to the longest proper suffix of its path that is still a node in the trie. A text character is consumed exactly once. On a missing edge the walk follows failure links,…
