当前位置:网站首页>119 · edit distance
119 · edit distance
2022-04-22 07:31:00 【yinhua405】
describe
Give two words word1 and word2, Work out what will be word1 Convert to word2 The minimum number of operations .
You can perform three operations :
- Insert a character
- Delete a character
- Replace a character
Wechat plus jiuzhang15 Send verification information 【 Big factories in China 】 Collar byte 、 Ali 、 Baidu and other latest high-frequency questions
len(word1),len(word2)<=500len(word1), len(word2) <= 500len(word1),len(word2)<=500
Examples
Examples 1:
Input :
word1 = "horse"
word2 = "ros"
Output :
3
explain :
horse -> rorse ( Replace 'h' by 'r')
rorse -> rose ( Delete 'r')
rose -> ros ( Delete 'e')
Examples 2:
Input :
word1 = "intention"
word2 = "execution"
Output :
5
explain :
intention -> inention ( Delete 't')
inention -> enention ( Replace 'i' by 'e')
enention -> exention ( Replace 'n' by 'x')
exention -> exection ( Replace 'n' by 'c')
exection -> execution ( Insert 'u')
int dp4(string word1, int s1, string word2, int s2, map<vector<int>, int> &memo)
{
if (s2 >= word2.size())
{
int tmp = word1.size() - s1;
tmp = tmp > 0 ? tmp : 0;
return tmp; // Delete the remaining
}
if (s1 >= word1.size())
{ // Insert remaining
int tmp = word2.size() - s2;
tmp = tmp > 0 ? tmp : 0;
return tmp;
}
vector<int>vec = { s1,s2 };
auto it = memo.find(vec);
if (it != memo.end())
{
return it->second;
}
if (word1[s1] == word2[s2])
{
memo[vec] = dp4(word1, s1+ 1, word2, s2 + 1, memo);
}
else
{
// Insert... Respectively , Delete , Replace
int insetCount = 1 + dp4(word1, s1, word2, s2 + 1, memo);// Insert
int deleteCount = 1 + dp4(word1, s1 + 1, word2, s2, memo); // Delete
int replaceCount = 1 + dp4(word1, s1 + 1, word2, s2 + 1, memo); // Replace
int tmpMin = min(insetCount, deleteCount);
memo[vec] = min(tmpMin, replaceCount);
}
return memo[vec];
}
int minDistance(string word1, string word2) {
map<vector<int>, int> memo;
return dp4(word1, 0, word2, 0,memo);
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220613324476.html
边栏推荐
- Leetcode - 5 - (repeated substring < KMP >, longest palindrome substring, transpose matrix, binary tree (left and right) view)
- L1-071 previous life files (20 points) (similar two points)
- Codeforces Round #774 (Div. 2)
- 189. Rotation array
- C language | preprocessing
- 1242 · non overlapping interval
- The art of concurrent programming (11): introduction to tool classes in JUC
- B. Ball Dropping (简单几何计算 / 相似三角形) (2021牛客暑期多校训练营1)
- H. Happy number (binary conversion / nth special number) (2021 Niuke summer multi school training camp 9)
- 2021 learning plan
猜你喜欢

Why is the data stored in the leaf node of the non primary key index the primary key value

快排与归并排序

The thought and code of eight sorting

Idea does not display the run dashboard view window

指针 结构体 const 小结

HDU Ice_cream‘s world I (并查集判环)

最小圆覆盖(计算几何基础)

I.Jam-packed (均分/最大最小值) (2021年度训练联盟热身训练赛第五场)

带环链表详解

C. Ducky debugging (Game 5 of 2021 training League warm-up training competition)
随机推荐
LeetCode - 6 - (字符串相乘、下一個更大元素<ⅠⅡⅢ>、k個一組翻轉鏈錶)
If I make this silly mistake again/ (ㄒoㄒ)/~~
牛客xb月赛45题解
Explanation and use of interface
437. 路径总和 III
E. Figure skiing (string sorting / check-in) (Game 5 of 2021 training League warm-up training competition)
Ansible的使用
要是我再犯这种憨憨错误就.../(ㄒoㄒ)/~~
363 · 接雨水
CF1547E Air Conditioners
D. Determine the Photo Position (简单找子串)(2021牛客暑期多校训练营1)
八大排序的思想及其代码
Codeforces Round #780 (Div. 3)
递归反转链表
Codeforces Round #588 (Div. 2) C D
189. 轮转数组
B. Ball Dropping (简单几何计算 / 相似三角形) (2021牛客暑期多校训练营1)
Codeforces Round #779 (Div. 2)
Codeforces Round #588 (Div. 2) C D
C.Ducky Debugging(简单判断/签到)(2021年度训练联盟热身训练赛第五场 )