// C# code for the above approach
using System;
class GFG
{
// Maximum 2-D array column size
static int maximum = 1000;
// Utility function to find minimum of three numbers
static int min(int x, int y, int z)
{
return Math.Min(Math.Min(x, y), z);
}
static int editDist(string str1, string str2, int m,
int n, int[,] dp)
{
// 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 the recursive call has been
// called previously, then return
// the stored value that was calculated
// previously
if (dp[m - 1, n - 1] != -1)
return dp[m - 1, n - 1];
// If last characters of two strings are same,
// nothing much to do. Ignore last characters and
// get count for remaining strings.
// Store the returned value at dp[m-1][n-1]
// considering 1-based indexing
if (str1[m - 1] == str2[n - 1])
return dp[m - 1, n - 1] = editDist(str1, str2, m - 1, n - 1, dp);
// 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.
// Store the returned value at dp[m-1][n-1]
// considering 1-based indexing
return dp[m - 1, n - 1] = 1 + min(editDist(str1, str2, m, n - 1,
dp), // Insert
editDist(str1, str2, m - 1, n,
dp), // Remove
editDist(str1, str2, m - 1, n - 1,
dp) // Replace
);
}
// Driver code
static void Main(string[] args)
{
string str1 = "sunday";
string str2 = "saturday";
int m = str1.Length;
int n = str2.Length;
// Declare a dp array which stores
// the answer to recursive calls
int[,] dp = new int[m, maximum];
for (int i = 0; i < dp.GetLength(0); i++)
for (int j = 0; j < dp.GetLength(1); j++)
dp[i, j] = -1;
// Function call
// memoization and top-down approach
Console.WriteLine(editDist(str1, str2, m, n, dp));
}
}
// This code is contributed by lokeshpotta20.