-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicsEdit.c
67 lines (55 loc) · 1.93 KB
/
DynamicsEdit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//QUESTION: Edit distance problem.
//CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function to find the minimum of three integers
int min(int x, int y, int z) {
if (x < y && x < z)
return x;
else if (y < x && y < z)
return y;
else
return z;
}
// Function to compute the edit distance between two strings
int editDistance(char *str1, char *str2, int m, int n) {
// Create a 2D array to store results of subproblems
int dp[m + 1][n + 1];
// Fill dp[][] in bottom-up manner
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
// If the first string is empty, insert all characters of the second string
if (i == 0)
dp[i][j] = j; // Minimum operations = j
// If the second string is empty, remove all characters of the first string
else if (j == 0)
dp[i][j] = i; // Minimum operations = i
// If the last characters are the same, ignore the last character
// and recur for the remaining substring
else if (str1[i - 1] == str2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
// If the last character is different, consider all possibilities
// and find the minimum among them
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() {
char str1[100], str2[100];
// Input strings
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
int m = strlen(str1);
int n = strlen(str2);
// Calculate and print the edit distance
printf("Edit Distance: %d\n", editDistance(str1, str2, m, n));
return 0;
}