forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain4.cpp
56 lines (40 loc) · 1.28 KB
/
main4.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// Source : https://leetcode.com/problems/delete-operation-for-two-strings/description/
/// Author : liuyubobobo
/// Time : 2017-11-10
#include <iostream>
#include <vector>
using namespace std;
/// Memory Search
/// Directly solve the minimum deleted character number problem
///
/// Time Complexity: O(len(word1)*len(word2))
/// Space Complexity: O(len(word2))
class Solution {
private:
vector<vector<int>> dp;
public:
int minDistance(string word1, string word2) {
dp.clear();
for(int i = 0 ; i < word1.size() ; i ++)
dp.push_back(vector<int>(word2.size(), -1));
return minDistance(word1, word1.size() - 1, word2, word2.size() - 1);
}
private:
int minDistance(const string& word1, int i1, const string& word2, int i2){
if(i1 < 0)
return i2 + 1;
if(i2 < 0)
return i1 + 1;
if(dp[i1][i2] != -1)
return dp[i1][i2];
int res = min(1 + minDistance(word1, i1 - 1, word2, i2),
1 + minDistance(word1, i1, word2, i2 - 1));
if(word1[i1] == word2[i2])
res = min(res, minDistance(word1, i1 - 1, word2, i2 - 1));
return dp[i1][i2] = res;
}
};
int main() {
cout << Solution().minDistance("sea", "eat") << endl;
return 0;
}