View source code
Display the source code in std/algorithm/comparison.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Function std.algorithm.comparison.levenshteinDistance
Returns the Levenshtein
distance between and s. The Levenshtein distance computes
the minimal amount of edit operations necessary to transform t
into s. Performs Ο(ts.length * t.length) evaluations of equals and occupies Ο(s.length * t.length) storage.
Prototype
size_t levenshteinDistance(alias equals, Range1, Range2)( Range1 s, Range2 t ) if (isForwardRange!Range1 && isForwardRange!Range2);
Parameters
| Name | Description |
|---|---|
| equals | The binary predicate to compare the elements of the two ranges. |
| s | The original range. |
| t | The transformation target |
Returns
The minimal number of edits to transform s into t.
Does not allocate GC memory.
Example
import std.algorithm.iteration : filter; import std.uni : toUpper; assert(levenshteinDistance("cat", "rat") == 1); assert(levenshteinDistance("parks", "spark") == 2); assert(levenshteinDistance("abcde", "abcde") == 0); assert(levenshteinDistance("abcde", "abCde") == 1); assert(levenshteinDistance("kitten", "sitting") == 3); assert(levenshteinDistance!((a, b) => std.uni.toUpper(a) == std.uni.toUpper(b)) ("parks", "SPARK") == 2); assert(levenshteinDistance("parks".filter!"true", "spark".filter!"true") == 2); assert(levenshteinDistance("ID", "I♥D") == 1);