Formatting

This commit is contained in:
Imbus 2024-11-21 08:49:45 +01:00
parent d10300509e
commit 70170ea995
2 changed files with 15 additions and 16 deletions

View file

@ -1,14 +1,13 @@
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string>
#include <vector>
int edit_distance(const std::string& s1, const std::string& s2) {
int edit_distance(const std::string &s1, const std::string &s2) {
size_t m = s1.size();
size_t n = s2.size();
// Create a 2D DP table
std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1));
std::vector<std::vector<size_t>> dp(m + 1, std::vector<size_t>(n + 1));
// Fill the base cases
for (size_t i = 0; i <= m; ++i)
@ -23,13 +22,14 @@ int edit_distance(const std::string& s1, const std::string& s2) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1]; // No operation needed
} else {
dp[i][j] = 1 + std::min({dp[i - 1][j], // Deletion
dp[i][j - 1], // Insertion
dp[i - 1][j - 1] // Substitution
});
dp[i][j] = 1 + std::min({
dp[i - 1][j], // Deletion
dp[i][j - 1], // Insertion
dp[i - 1][j - 1] // Substitution
});
}
}
}
return dp[m][n];
return static_cast<int>(dp[m][n]);
}

View file

@ -1,18 +1,17 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
/**
* @brief Computes the edit distance (Levenshtein distance) between two strings.
*
* The edit distance is defined as the minimum number of single-character edits
* (insertions, deletions, or substitutions) required to transform one string into the other.
* (insertions, deletions, or substitutions) required to transform one string
* into the other.
*
* This implementation uses dynamic programming to compute the distance efficiently.
* This implementation uses dynamic programming to compute the distance
* efficiently.
*
* @param s1 The first string.
* @param s2 The second string.
* @return The edit distance between the two strings.
*/
int edit_distance(const std::string& s1, const std::string& s2);
int edit_distance(const std::string &s1, const std::string &s2);