7 const std::size_t str1_len = str1.size();
8 const std::size_t str2_len = str2.size();
9 std::vector edit_distances(str1_len + 1, std::vector<int>(str2_len + 1, 0));
11 for (std::size_t i = 0; i < str1_len + 1; i++)
12 edit_distances[i][0] = i;
14 for (std::size_t j = 0; j < str2_len + 1; j++)
15 edit_distances[0][j] = j;
17 for (std::size_t i = 1; i < str1_len + 1; i++)
19 for (std::size_t j = 1; j < str2_len + 1; j++)
21 const int indicator = str1[i - 1] == str2[j - 1] ? 0 : 1;
22 edit_distances[i][j] = std::min({
23 edit_distances[i - 1][j] + 1,
24 edit_distances[i][j - 1] + 1,
25 edit_distances[i - 1][j - 1] + indicator
30 return edit_distances[str1_len][str2_len];
Lots of utilities about string, filesystem and more.
int levenshteinDistance(const std::string &str1, const std::string &str2)
Calculate the Levenshtein distance between two strings.