|
| 1 | +/* |
| 2 | +Given two words, word1 and word2, |
| 3 | +find min operations to convert word1 to word2 with some given set of rules: |
| 4 | +
|
| 5 | +1) insert a character |
| 6 | +2) delete a character |
| 7 | +3) replace a character |
| 8 | +
|
| 9 | +METHOD1: |
| 10 | +Naive approach: This approach is to check each and every possibility on a character. EG: |
| 11 | +Str1: data |
| 12 | +str2: dent |
| 13 | +We will compare the first two characters and if they are equal, if they are equal, then we |
| 14 | +simply move on to the next character, if they are not equal we will consider all the possibilities |
| 15 | +of deleting, replacing and inserting the element and compute the result recursively. In the end |
| 16 | +we check what method is taking minimum number of operations. |
| 17 | +
|
| 18 | +Time complexity: O(3^n) //n being the number of elements and there are 3 kind of operations that |
| 19 | +can be applied for each element |
| 20 | +Space complexity: O(1) |
| 21 | +
|
| 22 | +METHOD2: |
| 23 | +Dynamic Programming: Here in the above method if a tree is made for each possibility of replacing, |
| 24 | +inserting and deleting as branches we will get repeated sub problems, so in a way we can save them |
| 25 | +in a table and rather solve them in O(1) time. |
| 26 | +
|
| 27 | +Therefore we simply create a mXn matrix |
| 28 | +
|
| 29 | +c(i,j) = c(i-1,j-1) if match, |
| 30 | + if no match |
| 31 | + min{ |
| 32 | + 1 + c(i-1,j-1) if replaced, |
| 33 | + 1 + c(i-1,j) if delete, |
| 34 | + 1 + c(i,j-1) if insert |
| 35 | + } |
| 36 | +
|
| 37 | +Therefore, we make a matrix of size m + 1 X n+1 in case length of two strings is m X n. |
| 38 | +Now, imagine matrix like this |
| 39 | + 0 M A R C H |
| 40 | +0 0 1 2 3 4 5 //the first row and column means convert C to 0 converting CA to 0 and so on |
| 41 | +C 1 |
| 42 | +A 2 |
| 43 | +R 3 |
| 44 | +T 4 |
| 45 | +
|
| 46 | +If there is a match, we simply pick the diagnal element now when traversing, if there is no match, |
| 47 | +we take the min of top left and diagnal and add 1 to it. Its logical as if there is a match, the |
| 48 | +size of the problem is reduced by 1 on both sides as per recursive eqn. |
| 49 | +
|
| 50 | +If there is no match, we have to compute all problems as per recursive equation which has already been |
| 51 | +computed in the matrix |
| 52 | +
|
| 53 | +Time complexity: O(mn) |
| 54 | +Space complexity: O(mn) |
| 55 | +*/ |
| 56 | +#include <stdio.h> |
| 57 | +#include <stdlib.h> |
| 58 | +#include <string.h> |
| 59 | + |
| 60 | +int findMin(int a, int b, int c){ |
| 61 | + return (a < b)?((a<c)?a:c):((b<c)?b:c); |
| 62 | +} |
| 63 | + |
| 64 | +int findEditDistance(char *str1, char *str2){ |
| 65 | + int len1 = strlen(str1); |
| 66 | + int len2 = strlen(str2); |
| 67 | + printf("length of two strings are %d and %d\n", len1, len2); |
| 68 | + int editDis[len1 + 1][len2 + 1]; |
| 69 | + int i,j; |
| 70 | + for(i=0; i<=len1;i++){ |
| 71 | + for(j=0;j<=len2;j++){ |
| 72 | + if(i == 0){ |
| 73 | + editDis[i][j] = j; |
| 74 | + }else if(j == 0){ |
| 75 | + editDis[i][j] = i; |
| 76 | + }else if(str1[i-1] == str2[j-1]){ |
| 77 | + editDis[i][j] = editDis[i-1][j-1]; |
| 78 | + }else{ |
| 79 | + editDis[i][j] = 1 + findMin(editDis[i-1][j],editDis[i][j-1],editDis[i-1][j-1]); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return editDis[len1][len2]; |
| 84 | +} |
| 85 | + |
| 86 | +int main(){ |
| 87 | + char str1[] = "CART"; |
| 88 | + char str2[] = "MARCH"; |
| 89 | + |
| 90 | + int operations = findEditDistance(str1, str2); |
| 91 | + printf("min number of operations req are: %d\n", operations); |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments