|
| 1 | +/* |
| 2 | +Given two strings X and Y, find the minimum cost to make two strings equal using only delete operations. |
| 3 | +Cost to delete character in X is S and in Y is P |
| 4 | +
|
| 5 | +METHOD1: |
| 6 | +DP using LCS |
| 7 | +Here we need to minimize the number of delete operations, so that the cost can be minimized. |
| 8 | +Hence we need to find as many matching characters in both the strings as possible. |
| 9 | +One way is to find the longest common subsequence in both the strings and the remaining characters |
| 10 | +apart from those should be deleted. |
| 11 | +If the length of one strings is n and other is m |
| 12 | +
|
| 13 | +Time complexity: O(mn) //to find LCS |
| 14 | +Space complexity: O(mn) |
| 15 | +
|
| 16 | +If length of LCS is l |
| 17 | +Cost of deletion: (m-l)S + (n-l)P |
| 18 | +
|
| 19 | +METHOD2: |
| 20 | +Here we use DP: |
| 21 | +C(i,j) = { |
| 22 | + |
| 23 | + C(i-1,j-1) if s[i] == s[j], |
| 24 | + min{ C(i-1,j),(j-1,i) } , if not equal |
| 25 | +
|
| 26 | +
|
| 27 | +} |
| 28 | +
|
| 29 | +Therefore we need to minimize cost. Assume that the length of one string is n and other i m. In worst case |
| 30 | +C(i,j) can have m*n unique subproblems. A binary tree can be made to see the repeating sub problems as per the |
| 31 | +given recurive equation. Hence using bottom-up dynamic programming algo, we can solve this by making a 2d array |
| 32 | +with length 1 greater than the total length of the string. |
| 33 | +Here 0,1 means that we are comparing string with no letter with string with 1st letter and trying to find |
| 34 | +the answer (0,2): string with no letter with string with two letters and so on |
| 35 | +
|
| 36 | +Time complexity: O(mn) |
| 37 | +Space complexity: O(mn) |
| 38 | +*/ |
| 39 | + |
| 40 | +//METHOD2 |
| 41 | +#include <stdio.h> |
| 42 | +#include <stdlib.h> |
| 43 | +#include <string.h> |
| 44 | + |
| 45 | +int findMin(int a,int b){ |
| 46 | + return (a<b)?a:b; |
| 47 | +} |
| 48 | + |
| 49 | +int findCost(char *str1, char *str2, int cost1, int cost2){ |
| 50 | + int len1 = strlen(str1); |
| 51 | + int len2 = strlen(str2); |
| 52 | + |
| 53 | + int sol[len1+1][len2+1]; |
| 54 | + |
| 55 | + int i,j; |
| 56 | + |
| 57 | + sol[0][0] = 0; |
| 58 | + |
| 59 | + for(i=1;i<len1;i++){ |
| 60 | + sol[i][0] = cost1*i; |
| 61 | + } |
| 62 | + |
| 63 | + for(j=1;j<len1;j++){ |
| 64 | + sol[0][j] = cost2*j; |
| 65 | + } |
| 66 | + |
| 67 | + for(i=1;i<len1+1;i++){ |
| 68 | + for(j=1;j<len2+1;j++){ |
| 69 | + if(str1[i-1] == str2[j-1]){ |
| 70 | + sol[i][j] = sol[i-1][j-1]; |
| 71 | + }else{ |
| 72 | + sol[i][j] = findMin(sol[i-1][j]+1,sol[i][j-1] + 2); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + printf("priting the sol array\n"); |
| 78 | + |
| 79 | + for(i=0;i<len1+1;i++){ |
| 80 | + for(j=0;j<len2+1;j++){ |
| 81 | + printf("%d ", sol[i][j]); |
| 82 | + } |
| 83 | + printf("\n"); |
| 84 | + } |
| 85 | + |
| 86 | + return sol[len1][len2]; |
| 87 | +} |
| 88 | + |
| 89 | +int main(){ |
| 90 | + |
| 91 | + char str1[] = "RAVI"; |
| 92 | + int cost1 = 1; |
| 93 | + int cost2 = 2; |
| 94 | + char str2[] = "AIBD"; |
| 95 | + |
| 96 | + int cost = findCost(str1,str2, cost1, cost2); |
| 97 | + |
| 98 | + printf("min cost for making two strings equal is: %d\n", cost); |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | + |
0 commit comments