Skip to content

ML Models & Algorithms

N-gram Matching

In shortA fuzzy matching method that splits strings into overlapping chunks of n characters or words and scores similarity by how many chunks they share.

Updated 24 Oct 2026· 2 min read

N-gram matching is a fuzzy matching method that breaks strings into overlapping chunks of n characters or n words and scores similarity by how many chunks two strings share. It tolerates typos and partial matches, scales well to large lists, and is often the first step before more sophisticated matching.

How it works

An n-gram is a run of n consecutive units. Split “what is string matching in machine learning” into word bigrams and you get “what is”, “is string”, “string matching” and so on; trigrams give “what is string”, “is string matching” and so on. Character n-grams work the same way on letters, which is what makes the method typo-tolerant.

Once each string is a set of n-grams, similarity can be scored as the share of n-grams they have in common, for example Jaccard similarity (shared n-grams divided by all distinct n-grams), or by feeding character n-grams into TF-IDF with cosine similarity. PolyFuzz uses character trigrams with TF-IDF as its default model. Indexing n-grams means you only compare strings that share at least one chunk, which is why the method is so efficient.

How it’s used in SEO and marketing

  • Checking which titles or meta descriptions contain a key phrase, or part of it.
  • Finding articles that follow a pattern, such as how-to or ultimate-guide pages.
  • Near-duplicate and plagiarism detection.
  • Normalising hashtags, product names or campaign names.
  • Comparing your titles with a competitor’s to see the shared bigrams, trigrams and four-grams.

Example

MLforSEO’s fuzzy matching lesson splits “elephant” into character trigrams: ele, lep, eph, pha, han and ant. The misspelling “elefant” gives ele, lef, efa, fan and ant. They share two of nine distinct trigrams, a Jaccard similarity of about 0.22, while an exact match would score 0. Run the same comparison against a whole product catalogue and the misspelling still finds its closest candidates quickly.

Limitations and things to watch

  • No semantics: “cheap flights” and “low-cost airfare” share almost no n-grams.
  • The choice of n matters: small n is tolerant but noisy, large n is precise but brittle.
  • Very short strings produce few n-grams, so scores jump around.
  • Long strings produce many n-grams and become computationally heavy.

Learn it on MLforSEO

Where N-gram Matching shows up on MLforSEO

Sources and further reading

Other terms from ML Models & Algorithms

See all 55 ML Models & Algorithms terms →