Longest common subsequence
medium 20 minGiven two strings, find the longest sequence of characters that appears in both in the same order, gaps allowed.
Where it shows up
The same pattern appears across everyday software.
diff and git diff
The original Unix diff (Hunt and McIlroy, 1976) computed an LCS of the two files' lines: lines in the LCS are unchanged, the rest are shown as deletions and insertions. git uses Myers' algorithm, which finds the same kind of shortest edit script faster when the files are similar.
DNA and protein alignment
Comparing genes means lining up two sequences of letters with gaps. The Needleman–Wunsch algorithm fills the same two-string table, with scores for matches, mismatches and gaps instead of plain +1, to find the best global alignment.
Spell checkers and fuzzy search
Suggesting "receive" for "recieve" ranks dictionary words by edit distance, the sibling of LCS on the same table. Search boxes use it to forgive typos.
Plagiarism and code similarity
Tools that compare essays or source files look for long common subsequences of words or tokens, which survive light rewording and inserted lines better than exact substring matches.
Merging edits in version control
A three-way merge diffs each branch against the common ancestor, and those diffs come from LCS-style comparisons. Changes that touch different lines merge cleanly; overlapping ones become conflicts.