Skip to content

Commit

Permalink
Create MinCostToMakeStringsEqual.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored May 26, 2024
1 parent 906aa8b commit e554844
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions DP/MinCostToMakeStringsEqual.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {

public:

int lcs(string x,string y,int n,int m){
vector<vector<int>>dp(n+1,vector<int>(m+1,0));
for(int i=0;i<=n;i++){
dp[i][0]=0;
}
for(int j=0;j<=m;j++){
dp[0][j]=0;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(x[i-1]==y[j-1]){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]= max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[n][m];
}
int findMinCost(string x, string y, int costX, int costY) {
// Your code goes here
int n=x.size(),m=y.size();
return ((n-lcs(x,y,n,m))*costX)+((m-lcs(x,y,n,m))*costY);

}
};

0 comments on commit e554844

Please sign in to comment.