A developer's guide to text comparison algorithms. Learn how Git and diff checkers find line changes using the Longest Common Subsequence and Myers diff.
Calculate your numbers instantly in your browser. Fast, free, no signup.
Every software engineer interacts with text diffing constantly: running git diff, reviewing GitHub pull requests, resolving merge conflicts, and inspecting database schema changes.
When two versions of a document or source file are compared, how does a computer determine the minimal set of line additions, deletions, and modifications needed to transform Version A into Version B?
Comparing two texts is not as simple as checking if line 1 matches line 1. If you insert a single new line at the very top of a 1,000-line file:
Finding this optimal alignment is an instance of the Shortest Edit Script (SES) problem, which is mathematically dual to finding the Longest Common Subsequence (LCS).
A subsequence is a sequence that appears in the same relative order, but not necessarily contiguously.
Consider two strings:
A B C D G HA E D F H RThe Longest Common Subsequence between them is A D H (length 3).
- B, - C, - G).+ E, + F, + R).In 1986, computer scientist Eugene W. Myers published a breakthrough paper: "An O(ND) Difference Algorithm and Its Variations".
Myers framed the diff problem as finding the shortest path on an edit graph:
Because typical code edits involve only a few changed lines ($D$) among thousands of total lines ($N$), Myers' algorithm scales with the number of differences rather than file length, making it extraordinarily fast and memory-efficient.
The standard text representation for diffs popularized by Unix and Git is the Unified Diff Format:
@@ -1,4 +1,5 @@
import React from 'react'
-import { LegacyButton } from './legacy'
+import { Button } from '@/components/ui'
+import { Tooltip } from '@/components/ui'
export default App
- (red) were deleted.+ (green) were added.Need to quickly inspect the differences between two contracts, markdown articles, or configuration files? Use our free Diff Checker. It uses local browser execution to compute additions and deletions without transmitting your sensitive text to any remote server.