class Solution {
public:
int minDistance(string word1, string word2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len1 = word1.size()+1;
int len2 = word2.size()+1;
if (len1==1) {
return len2-1;
}
if (len2==1) {
return len1-1;
}
int LCS[len1][len2];
for (int i=0; i <len1; i++) {
LCS[i][0] = i;
}
for (int j=0; j < len2; j++) {
LCS[0][j] = j;
}
for (int i=1; i<len1; i++) {
for(int j=1; j<len2; j++) {
LCS[i][j] =INT_MAX;
if (word1[i-1] == word2[j-1]) {
LCS[i][j] = LCS[i-1][j-1];
} else {
LCS[i][j] = min((LCS[i-1][j-1]+1),LCS[i][j]); //replace
LCS[i][j] = min(LCS[i][j], min(LCS[i-1][j], LCS[i][j-1]) + 1);
}
}
}
return LCS[len1-1][len2-1];
}
};
public:
int minDistance(string word1, string word2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len1 = word1.size()+1;
int len2 = word2.size()+1;
if (len1==1) {
return len2-1;
}
if (len2==1) {
return len1-1;
}
int LCS[len1][len2];
for (int i=0; i <len1; i++) {
LCS[i][0] = i;
}
for (int j=0; j < len2; j++) {
LCS[0][j] = j;
}
for (int i=1; i<len1; i++) {
for(int j=1; j<len2; j++) {
LCS[i][j] =INT_MAX;
if (word1[i-1] == word2[j-1]) {
LCS[i][j] = LCS[i-1][j-1];
} else {
LCS[i][j] = min((LCS[i-1][j-1]+1),LCS[i][j]); //replace
LCS[i][j] = min(LCS[i][j], min(LCS[i-1][j], LCS[i][j-1]) + 1);
}
}
}
return LCS[len1-1][len2-1];
}
};