Formatting
This commit is contained in:
parent
d10300509e
commit
70170ea995
2 changed files with 15 additions and 16 deletions
|
@ -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,7 +22,8 @@ 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 + std::min({
|
||||
dp[i - 1][j], // Deletion
|
||||
dp[i][j - 1], // Insertion
|
||||
dp[i - 1][j - 1] // Substitution
|
||||
});
|
||||
|
@ -31,5 +31,5 @@ int edit_distance(const std::string& s1, const std::string& s2) {
|
|||
}
|
||||
}
|
||||
|
||||
return dp[m][n];
|
||||
return static_cast<int>(dp[m][n]);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue