// Cpp program to find minimum number operations
// to convert s1 to s2 using Memoization
#include <bits/stdc++.h>
using namespace std;
// Maximum 2-D array column size
int maximum = 1000;
// 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 s1, string s2, int m, int n, vector<vector>&dp)
{
// If first string is empty, the only option is to
// insert all characters of second string into first
if (m == 0)
return n;
code
// 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 (s1[m - 1] == s2[n - 1])
return dp[m - 1][n - 1] = editDist(s1, s2, 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(s1, s2, m, n - 1, dp), // Insert
editDist(s1, s2, m - 1, n, dp), // Remove
editDist(s1, s2, m - 1, n - 1, dp) // Replace
);
}
int main()
{
code
Assistant
string s1 = "sunday";
string s2 = "saturday";
int m = s1.length();
int n = s2.length();
// Declare a dp array which stores
// the answer to recursive calls
vector<vector<int>>dp(m,vector<int>(maximum,-1));
cout << editDist(s1, s2, m, n, dp);
return 0;
}
Give me code in Python, Java, C#, JavaScript
Remember like Cpp is for C++ and at start replace Cpp with language name and then comment
without changing variable name and also do not change comments
have one line space after code when comment is written.
don’t use class if needed then name it GfG
don’t use public if not needed.
and In C program don’t use #define and malloc and calloc instead use simple array
python
# Python program to find minimum number operations
# to convert s1 to s2 using Memoization
import sys
# Maximum 2-D array column size
maximum = 1000
# Utility function to find minimum of three numbers
def min(x, y, z):
return min(min(x, y), z)
def editDist(s1, s2, m, n, 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 s1[m - 1] == s2[n - 1]:
dp[m - 1][n - 1] = editDist(s1, s2, m - 1, n - 1, dp)
return dp[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.
# Store the returned value at dp[m-1][n-1]
# considering 1-based indexing
dp[m - 1][n - 1] = 1 + min(
editDist(s1, s2, m, n - 1, dp), # Insert
editDist(s1, s2, m - 1, n, dp), # Remove
editDist(s1, s2, m - 1, n - 1, dp) # Replace
)
return dp[m - 1][n - 1]
def main():
s1 = "sunday"
s2 = "saturday"
m = len(s1)
n = len(s2)
# Declare a dp array which stores
# the answer to recursive calls
dp = [[-1 for _ in range(maximum)] for _ in range(m)]
print(editDist(s1, s2, m, n, dp))
if __name__ == "__main__":
main()
java
// Java program to find minimum number operations
// to convert s1 to s2 using Memoization
import java.util.Arrays;
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 s1, String s2, 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 (s1.charAt(m - 1) == s2.charAt(n - 1)) {
dp[m - 1][n - 1] = editDist(s1, s2, m - 1, n - 1, dp);
return dp[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.
// Store the returned value at dp[m-1][n-1]
// considering 1-based indexing
dp[m - 1][n - 1] = 1 + min(
editDist(s1, s2, m, n - 1, dp), // Insert
editDist(s1, s2, m - 1, n, dp), // Remove
editDist(s1, s2, m - 1, n - 1, dp) // Replace
);
return dp[m - 1][n - 1];
}
public static void main(String[] args) {
String s1 = "sunday";
String s2 = "saturday";
int m = s1.length();
int n = s2.length();
// Declare a dp array which stores
// the answer to recursive calls
int[][] dp = new int[m][maximum];
for (int[] row : dp)
Arrays.fill(row, -1);
System.out.println(editDist(s1, s2, m, n, dp));
}
}
csharp
// C# program to find minimum number operations
// to convert s1 to s2 using Memoization
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 s1, string s2, 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 (s1[m - 1] == s2[n - 1])
{
dp[m - 1, n - 1] = EditDist(s1, s2, m - 1, n - 1, dp);
return dp[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.
// Store the returned value at dp[m-1][n-1]
// considering 1-based indexing
dp[m - 1, n - 1] = 1 + Min(
EditDist(s1, s2, m, n - 1, dp), // Insert
EditDist(s1, s2, m - 1, n, dp), // Remove
EditDist(s1, s2, m - 1, n - 1, dp) // Replace
);
return dp[m - 1, n - 1];
}
static void Main(string[] args)
{
string s1 = "sunday";
string s2 = "saturday";
int m = s1.Length;
int n = s2.Length;
// Declare a dp array which stores
// the answer to recursive calls
int[,] dp = new int[m, maximum];
for (int i = 0; i < m; i++)
for (int j = 0; j < maximum; j++)
dp[i, j] = -1;
Console.WriteLine(EditDist(s1, s2, m, n, dp));
}
}
javascript
// JavaScript program to find minimum number operations
// to convert s1 to s2 using Memoization
const maximum = 1000;
// Utility function to find minimum of three numbers
function min(x, y, z) {
return Math.min(Math.min(x, y), z);
}
function editDist(s1, s2, m, n, 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 (s1[m - 1] === s2[n - 1]) {
dp[m - 1][n - 1] = editDist(s1, s2, m - 1, n - 1, dp);
return dp[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.
// Store the returned value at dp[m-1][n-1]
// considering 1-based indexing
dp[m - 1][n - 1] = 1 + min(
editDist(s1, s2, m, n - 1, dp), // Insert
editDist(s1, s2, m - 1, n, dp), // Remove
editDist(s1, s2, m - 1, n - 1, dp) // Replace
);
return dp[m - 1][n - 1];
}
function main() {
const s1 = "sunday";
const s2 = "saturday";
const m = s1.length;
const n = s2.length;
// Declare a dp array which stores
// the answer to recursive calls
const dp = Array.from({ length: m }, () => Array(maximum).fill(-1));
console.log(editDist(s1, s2, m, n, dp));
}
main();