Skip to content

levenshteinDistance

Levenshtein edit distance between two strings — the minimum number of single-character insertions, deletions, or substitutions needed to turn a into b.

Available since v3.1.1

import { levenshteinDistance } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { levenshteinDistance } from 'helpers4/string';
levenshteinDistance(a: string, b: string, caseSensitive: boolean): number
ParameterTypeDescription
astringThe first string
bstringThe second string
caseSensitivebooleanWhether the comparison is case-sensitive. Defaults to `true`.

number — The edit distance; 0 when a and b are equal (after case-folding when caseSensitive is false)

“kitten” becomes “sitting” with 3 single-character edits.

levenshteinDistance('kitten', 'sitting')
// => 3

Pass false to ignore letter case when comparing.

levenshteinDistance('Kitten', 'kitten', false)
// => 0

View source on GitHub