Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int LevenshteinDistance(const std::string &first_string, const std::string &second_string) {
- std::vector<std::vector<int>> dynamic(first_string.size() + 1,
- std::vector<int>(second_string.size() + 1));
- for (int index = 0; index <= static_cast<int>(first_string.size()); ++index) {
- dynamic[index][0] = index;
- }
- for (int index = 0; index <= static_cast<int>(second_string.size()); ++index) {
- dynamic[0][index] = index;
- }
- for (size_t index_first = 1; index_first <= first_string.size(); ++index_first) {
- for (size_t index_second = 1; index_second <= second_string.size(); ++index_second) {
- if (first_string[index_first - 1] != second_string[index_second - 1]) {
- dynamic[index_first][index_second] =
- Min(1 + dynamic[index_first - 1][index_second],
- 1 + dynamic[index_first][index_second - 1],
- 1 + dynamic[index_first - 1][index_second - 1]);
- } else {
- dynamic[index_first][index_second] = dynamic[index_first - 1][index_second - 1];
- }
- }
- }
- return dynamic.back().back();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement