Post

Edit distance

Edit distance

Problem

This problem was asked by Google.

The edit distance between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other. For example, the edit distance between “kitten” and “sitting” is three: substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”.

Given two strings, compute the edit distance between them.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int edit_distance(const string a, const string b) {
    if (a.empty()) return b.size();
    if (b.empty()) return a.size();
    int size_a = a.size();
    int size_b = b.size();
    vector<vector<int>> distance(size_a+1, vector<int>(size_b+1, 0));
    for (int i = 0; i <= size_a; ++i) {
        distance[i][0] = i;
    }
    for (int i = 0; i <= size_b; ++i) {
        distance[0][i] = i;
    }
    for (int i = 1; i <= size_a; ++i) {
        for (int j = 1; j <= size_b; ++j) {
            bool same = a[i-1] != b[j-1];
            distance[i][j] = min({distance[i-1][j-1] + same, distance[i-1][j] + 1, distance[i][j-1]+1});
        }
    }
    return distance[size_a][size_b];
}

void test(const string a, const string b) {
    cout << "Distance: " << a << " -- " << b << " = " << edit_distance(a, b) << endl;
}

int main() {
    test("abc", "abc");
    test("a", "abd");
    test("kitten", "sitting");
    return 0;
}
This post is licensed under CC BY 4.0 by the author.