From 48053e440944afd6d40644aad3c6f700d8cd72c3 Mon Sep 17 00:00:00 2001 From: Srishti Dubey <56927445+srieeee@users.noreply.github.com> Date: Wed, 23 Oct 2019 21:03:12 +0530 Subject: [PATCH] adding edit distance problem --- .../dynamic programming/edist.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Data Structure & Algorithm/dynamic programming/edist.cpp diff --git a/Data Structure & Algorithm/dynamic programming/edist.cpp b/Data Structure & Algorithm/dynamic programming/edist.cpp new file mode 100644 index 0000000..8b6d287 --- /dev/null +++ b/Data Structure & Algorithm/dynamic programming/edist.cpp @@ -0,0 +1,50 @@ +// A Naive recursive C++ program to find minimum number +// operations to convert str1 to str2 +#include +using namespace std; + +// Utility function to find minimum of three numbers +int min(int x, int y, int z) +{ + return min(min(x, y), z); +} + +int editDist(string str1, string str2, int m, int n) +{ + // If first string is empty, the only option is to + // insert all characters of second string into first + if (m == 0) + return n; + + // If second string is empty, the only option is to + // remove all characters of first string + if (n == 0) + return m; + + // If last characters of two strings are same, nothing + // much to do. Ignore last characters and get count for + // remaining strings. + if (str1[m - 1] == str2[n - 1]) + return editDist(str1, str2, m - 1, n - 1); + + // If last characters are not same, consider all three + // operations on last character of first string, recursively + // compute minimum cost for all three operations and take + // minimum of three values. + return 1 + min(editDist(str1, str2, m, n - 1), // Insert + editDist(str1, str2, m - 1, n), // Remove + editDist(str1, str2, m - 1, n - 1) // Replace + ); +} + +// Driver program +int main() +{ + // your code goes here + string str1 = "sunday"; + string str2 = "saturday"; + + cout << editDist(str1, str2, str1.length(), str2.length()); + + return 0; +}