Blog
Wild & Free Tools

How a Text Diff Tool Works: The Longest Common Subsequence Algorithm

Last updated: April 2026 6 min read
Quick Answer

Table of Contents

  1. What LCS actually does
  2. Why LCS specifically
  3. Walkthrough with a small example
  4. Why diff output sometimes looks weird
  5. Performance and limits
  6. Frequently Asked Questions

Every text diff tool — including the free tool — works by finding the Longest Common Subsequence (LCS) between two texts. The algorithm identifies every line that appears in both the original and the revised version in the same order, marks those as unchanged, and marks everything else as added or removed. Here is a plain-English explanation of how it works and why diff output sometimes looks unexpected.

The Core Idea: Longest Common Subsequence

Given two sequences — for our purposes, two lists of lines — LCS finds the longest ordered list of items that appears in both sequences.

Example with simple sequences:

For text diff, the "items" are lines. LCS identifies the lines that appear in both texts in the same relative order. Those are the "unchanged" lines. Lines only in the original become "removed." Lines only in the revised become "added."

Why LCS Is the Right Algorithm for Diff

Other algorithms could answer "what changed?" differently. LCS is the standard because it minimizes the total number of changes shown:

This minimizes output size, which is why LCS diff is the algorithm behind GNU diff, git diff, and most text comparison tools.

Sell Custom Apparel — We Handle Printing & Free Shipping

A Worked Example with Actual Text

Original text:

1. Dear team,
2. The meeting is at 3pm.
3. Please bring your laptops.
4. Regards,
5. Alex

Revised text:

1. Dear team,
2. The meeting is at 4pm.
3. Please bring your laptops.
4. And your notebooks.
5. Regards,
6. Alex

LCS of the two: [Dear team, Please bring your laptops, Regards, Alex] — 4 lines that appear in both in order.

Diff output:

When Diff Output Surprises You

Occasionally diff output looks unexpected — lines marked as changed when they seem identical. Common causes:

When diff output looks wrong, the most common culprit is invisible character differences in otherwise-identical-looking lines.

How Fast Is LCS on Large Texts?

LCS has quadratic complexity — O(n*m) where n and m are the lengths of the two sequences. For line-level diff with a few hundred lines, this is instant. For very long texts with many thousands of lines, you may notice a brief pause.

Practical limits for browser-based diff:

Modern browsers handle typical document sizes without problems. Only extreme cases (entire codebases, massive log files) run into performance limits.

See LCS in Action

Paste two texts, compare. Watch the LCS algorithm find what stayed the same and what changed.

Open Free Text Diff Tool

Frequently Asked Questions

What algorithm does diff use?

Most diff tools, including the browser-based text diff, use the Longest Common Subsequence (LCS) algorithm. GNU diff, git diff, and professional comparison tools all rely on LCS or variants.

Why does LCS minimize the diff?

By finding the longest common subsequence, LCS identifies the maximum amount of unchanged content — which minimizes what has to be marked as added or removed. This produces the smallest accurate diff.

Can LCS handle very large texts?

LCS has quadratic complexity, so extremely large texts (tens of thousands of lines) can slow down. Modern browsers handle up to roughly 50,000 lines without problems.

Is there a faster diff algorithm than LCS?

Specialized algorithms like Myers diff (used by git) optimize LCS for typical real-world cases, producing results faster. They still rely on the LCS concept at their core.

Ashley Connors
Ashley Connors Content Strategy & Writing Writer

Ashley has been a freelance copywriter and content strategist for eight years across e-commerce, SaaS, and media.

More articles by Ashley →
Launch Your Own Clothing Brand — No Inventory, No Risk