24 bool allow_replacements,
25 int max_edit_distance) {
41 vector<int> row(n + 1);
42 for (
int i = 1; i <= n; ++i)
45 for (
int y = 1; y <= m; ++y) {
47 int best_this_row = row[0];
50 for (
int x = 1; x <= n; ++x) {
52 if (allow_replacements) {
53 row[x] = min(previous + (s1.
str_[y - 1] == s2.
str_[x - 1] ? 0 : 1),
54 min(row[x - 1], row[x]) + 1);
57 if (s1.
str_[y - 1] == s2.
str_[x - 1])
60 row[x] = min(row[x - 1], row[x]) + 1;
63 best_this_row = min(best_this_row, row[x]);
66 if (max_edit_distance && best_this_row > max_edit_distance)
67 return max_edit_distance + 1;
StringPiece represents a slice of a string whose memory is managed externally.
int EditDistance(const StringPiece &s1, const StringPiece &s2, bool allow_replacements, int max_edit_distance)