freeCodeCamp/curriculum/challenges/german/10-coding-interview-prep/rosetta-code/levenshtein-distance.md

2.7 KiB

id title challengeType forumTopicId dashedName
5e4ce2eaac708cc68c1df260 Levenshtein distance 1 385264 levenshtein-distance

--description--

In information theory and computer science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (i.e. an edit distance). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.

Example:

The Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits:

  • kitten   sitten    (substitution of 'k' with 's')
  • sitten   sittin    (substitution of 'e' with 'i')
  • sittin   sitting    (insert 'g' at the end).

The Levenshtein distance between "rosettacode", "raisethysword" is 8.

The distance between two strings is same as that when both strings are reversed.

--instructions--

Write a function that returns the Levenshtein distance between two strings given as parameters.

--hints--

levenshtein should be a function.

assert(typeof levenshtein == 'function');

levenshtein("mist", "dist") should return a number.

assert(typeof levenshtein('mist', 'dist') == 'number');

levenshtein("mist", "dist") should return 1.

assert.equal(levenshtein('mist', 'dist'), 1);

levenshtein("tier", "tor") should return 2.

assert.equal(levenshtein('tier', 'tor'), 2);

levenshtein("kitten", "sitting") should return 3.

assert.equal(levenshtein('kitten', 'sitting'), 3);

levenshtein("stop", "tops") should return 2.

assert.equal(levenshtein('stop', 'tops'), 2);

levenshtein("rosettacode", "raisethysword") should return 8.

assert.equal(levenshtein('rosettacode', 'raisethysword'), 8);

levenshtein("mississippi", "swiss miss") should return 8.

assert.equal(levenshtein('mississippi', 'swiss miss'), 8);

--seed--

--seed-contents--

function levenshtein(a, b) {

}

--solutions--

function levenshtein(a, b) {
  var t = [], u, i, j, m = a.length, n = b.length;
  if (!m) { return n; }
  if (!n) { return m; }
  for (j = 0; j <= n; j++) { t[j] = j; }
  for (i = 1; i <= m; i++) {
    for (u = [i], j = 1; j <= n; j++) {
      u[j] = a[i - 1] === b[j - 1] ? t[j - 1] : Math.min(t[j - 1], t[j], u[j - 1]) + 1;
    } t = u;
  } return u[n];
}