[leetcode]583. Delete Operation for Two Strings
阿新 • • 發佈:2018-11-09
[leetcode]583. Delete Operation for Two Strings
Analysis
好冷鴨—— [每天刷題並不難0.0]
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.
求最長相同子串
Implement
class Solution {
public:
int minDistance(string word1, string word2) {
int len1 = word1.size();
int len2 = word2.size();
vector<vector<int>> dp(len1+1, vector<int>(len2+1, 0));
for(int i=1; i<=len1; i++){
for(int j=1; j<=len2; j++){
if (word1[i-1] == word2[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
return len1-dp[len1][len2]+len2-dp[len1][len2];
}
};