# 72. Edit Distance

## 刷题内容

• https://leetcode.com/problems/edit-distance/

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

Example 1:

Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:

Input:
s = "wordgoodstudentgoodword",
words = ["word","student"]
Output: []



class Solution {
public:
vector<vector<int> > dp;
int minLen(string& w1,string& w2,int i,int j)
{
if(i < 0)
return j + 1;
if(j < 0)
return i + 1;
if(dp[i][j])
return dp[i][j];
if(w1[i] == w2[j])
{
dp[i][j] = minLen(w1,w2,i - 1,j - 1);
return dp[i][j];
}
int temp1 = min(minLen(w1,w2,i - 1,j) + 1,minLen(w1,w2,i - 1,j - 1) + 1);
dp[i][j] = min(minLen(w1,w2,i,j - 1) + 1,temp1);
return dp[i][j];
}
int minDistance(string word1, string word2) {
int i = word1.length() - 1,j = word2.length() - 1;
for(int t1 = 0;t1 <= i;++t1)
{
vector<int> v1;
for(int t2 = 0;t2 <= j;++t2)
v1.push_back(0);
dp.push_back(v1);
}
int m = minLen(word1,word2,i,j);
return m;
}
};