-
Notifications
You must be signed in to change notification settings - Fork 481
/
0072.cpp
31 lines (31 loc) · 877 Bytes
/
0072.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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int minDistance(string word1, string word2)
{
int m = word1.size(), n = word2.size();
vector<vector<int>> mem(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++) mem[i][0] = i;
for (int j = 1; j <= n; j++) mem[0][j] = j;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
mem[i][j] = min(mem[i - 1][j - 1] + (word1[i-1] != word2[j-1]), min(mem[i][j - 1] + 1, mem[i - 1][j] + 1));
}
}
return mem[m][n];
}
};
int main()
{
string word1 = "";
string word2 = "";
cout << Solution().minDistance(word1, word2) << endl;
return 0;
}