From 0607b7cbdc59d13503da55b094139c2130456dcb Mon Sep 17 00:00:00 2001 From: sakssingh <85640472+sakssingh@users.noreply.github.com> Date: Tue, 14 Sep 2021 00:31:10 +0530 Subject: [PATCH] chore(CPlusPlus): Add Edit Distance (#458) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- .../Dynamic-Programming/edit-distance.cpp | 72 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 73 insertions(+) create mode 100644 algorithms/CPlusPlus/Dynamic-Programming/edit-distance.cpp diff --git a/algorithms/CPlusPlus/Dynamic-Programming/edit-distance.cpp b/algorithms/CPlusPlus/Dynamic-Programming/edit-distance.cpp new file mode 100644 index 00000000..6eeb8996 --- /dev/null +++ b/algorithms/CPlusPlus/Dynamic-Programming/edit-distance.cpp @@ -0,0 +1,72 @@ +// Given two strings 's1' and 's2' and below operations that can performed on 's1'. +// 1.Insert +// 2.Remove +// 3.Replace +//Find minimum number of edits (operations) required to convert ‘s1’ into ‘s2’. + +// NOTE: All above operations are of equal cost +// Input: s1 = "car", s2 = "cars" +// Output: 1 +// We can convert s1 into s2 by inserting a 's'. + +//Program using Dynamic Programming +//TIME COMPLEXITY: O(m*n) + +#include +#include +using namespace std; + +// Utility function to find the minimum of three numbers +int min(int x, int y, int z) { return min(min(x, y), z); } + +int editDistDP(string s1, string s2, int m, int n) +{ + vector> dp(m + 1, vector(n + 1)); + + for (int i = 0; i <= m; i++) { + for (int j = 0; j <= n; j++) { + // If first string is empty, only option is to + // insert all characters of second string + if (i == 0) + dp[i][j] = j; // Min. operations = j + + // If second string is empty, only option is to + // remove all characters of second string + else if (j == 0) + dp[i][j] = i; // Min. operations = i + + // If last characters are same, ignore last char + // and recur for remaining string + else if (s1[i - 1] == s2[j - 1]) + dp[i][j] = dp[i - 1][j - 1]; + + // If the last character is different, consider + // all possibilities and find the minimum + else + dp[i][j] + = 1 + + min(dp[i][j - 1], // Insert + dp[i - 1][j], // Remove + dp[i - 1][j - 1]); // Replace + } + } + + return dp[m][n]; +} + +int main() +{ + string s1, s2; + cout << "Enter 1st string: " << '\n'; + cin >> s1; + cout << "Enter 2nd string: " << '\n'; + cin >> s2; + int n = s1.length(); + int m = s2.length(); + cout << editDistDP(s1, s2, n, m); + return 0; +} + +// s1: food +// s2: money +// output: 4 diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 393d9be7..bae45a72 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -21,6 +21,7 @@ 2. [Longest Common Substring](Dynamic-Programming/longest-common-substring.cpp) 3. [0/1-knapsack](Dynamic-Programming/01-knapsack.cpp) 4. [Matrix chain Multiplication](Dynamic-Programming/matrix-chain-multiplication.cpp) +5. [Edit Distance](Dynamic-Programming/edit-distance.cpp) ## Graphs