forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
34 lines (26 loc) · 947 Bytes
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/// Source : https://leetcode.com/problems/delete-operation-for-two-strings/description/
/// Author : liuyubobobo
/// Time : 2017-11-10
#include <iostream>
#include <vector>
using namespace std;
/// LCS - Dynamic Programming
/// Time Complexity: O(len(word1)*len(word2))
/// Space Complexity: O(len(word1)*len(word2))
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1, 0));
for(int i = 1 ; i <= word1.size() ; i ++)
for(int j = 1 ; j <= word2.size() ; j ++){
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
if(word1[i-1] == word2[j-1])
dp[i][j] = max(dp[i][j], 1 + dp[i-1][j-1]);
}
return word1.size() + word2.size() - 2 * dp[word1.size()][word2.size()];
}
};
int main() {
cout << Solution().minDistance("sea", "eat") << endl;
return 0;
}