← All tools

Longest Common Subsequence

Find the longest common subsequence (LCS) of two texts and show a diff-style alignment. Unlike edit distance, the LCS is the longest sequence of tokens that appears in both inputs in order (not necessarily contiguously) — the basis of line-by-line diff. Choose character, word, or line granularity, and toggle case sensitivity. Everything runs locally in your browser.

Inputs

Result

LCS

Alignment

■ common■ only in A (deleted)■ only in B (added)

The longest common subsequence of two sequences is the longest sequence that appears in both as an ordered (not necessarily contiguous) subsequence. It is found with dynamic programming: dp[i][j] is the LCS length of the first i tokens of A and first j of B; if a[i-1] === b[j-1] it extends the diagonal, otherwise it takes the best of dropping one token. Backtracking the table recovers the actual subsequence and a diff alignment. This is the core of the classic diff utility. The similarity ratio reported is 2·LCS / (|A| + |B|). Distinct from the Edit Distance tool, which counts minimal edits (insert/delete/substitute) rather than returning the shared sequence. Pairs with the Text Diff and Edit Distance tools. Everything runs locally — nothing leaves your browser.