Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.
You have the following three operations permitted on a word:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
0 <= word1.length, word2.length <= 500word1 and word2 consist of lowercase English letters.No developer has claimed the Java throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
It is a question of comparing two strings. After doing similar questions many times, we will develop an intuition to use Dynamic Programming with two-dimensional arrays.
class Solution {
public int minDistance(String word1, String word2) {
var dp = new int[word1.length() + 1][word2.length() + 1];
for (var i = 0; i < dp.length; i++) {
dp[i][0] = i;
}
for (var j = 0; j < dp[0].length; j++) {
dp[0][j] = j;
}
for (var i = 1; i < dp.length; i++) {
for (var j = 1; j < dp[0].length; j++) {
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
}
}
}
return dp[dp.length - 1][dp[0].length - 1];
}
}
No developer has claimed the C# throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
It is a question of comparing two strings. After doing similar questions many times, we will develop an intuition to use Dynamic Programming with two-dimensional arrays.
public class Solution
{
public int MinDistance(string word1, string word2)
{
var dp = new int[word1.Length + 1, word2.Length + 1];
for (var i = 0; i < dp.GetLength(0); i++)
dp[i, 0] = i;
for (var j = 0; j < dp.GetLength(1); j++)
dp[0, j] = j;
for (var i = 1; i < dp.GetLength(0); i++)
{
for (var j = 1; j < dp.GetLength(1); j++)
{
if (word1[i - 1] == word2[j - 1])
{
dp[i, j] = dp[i - 1, j - 1];
}
else
{
dp[i, j] = Math.Min(dp[i - 1, j - 1], Math.Min(dp[i - 1, j], dp[i, j - 1])) + 1;
}
}
}
return dp[dp.GetUpperBound(0), dp.GetUpperBound(1)];
}
}
No developer has claimed the Python throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
It is a question of comparing two strings. After doing similar questions many times, we will develop an intuition to use Dynamic Programming with two-dimensional arrays.
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
dp = [[0] * (len(word2) + 1) for _ in range(len(word1) + 1)]
for i in range(len(dp)):
dp[i][0] = i
for j in range(len(dp[0])):
dp[0][j] = j
for i in range(1, len(dp)):
for j in range(1, len(dp[0])):
if word1[i - 1] == word2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
return dp[-1][-1]
No developer has claimed the C++ throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
It is a question of comparing two strings. After doing similar questions many times, we will develop an intuition to use Dynamic Programming with two-dimensional arrays.
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1));
for (auto i = 0; i < dp.size(); i++) {
dp[i][0] = i;
}
for (auto j = 0; j < dp[0].size(); j++) {
dp[0][j] = j;
}
for (auto i = 1; i < dp.size(); i++) {
for (auto j = 1; j < dp[0].size(); j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min({dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]}) + 1;
}
}
}
return dp[dp.size() - 1][dp[0].size() - 1];
}
};
No developer has claimed the Javascript throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
It is a question of comparing two strings. After doing similar questions many times, we will develop an intuition to use Dynamic Programming with two-dimensional arrays.
var minDistance = function (word1, word2) {
const dp = Array(word1.length + 1).fill().map(
() => Array(word2.length + 1).fill(0)
)
dp.forEach((_, i) => { dp[i][0] = i })
dp[0].forEach((_, j) => { dp[0][j] = j })
for (let i = 1; i < dp.length; i++) {
for (let j = 1; j < dp[0].length; j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1]
} else {
dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
}
}
}
return dp.at(-1).at(-1)
};
No developer has claimed the Go throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
It is a question of comparing two strings. After doing similar questions many times, we will develop an intuition to use Dynamic Programming with two-dimensional arrays.
func minDistance(word1 string, word2 string) int {
dp := make([][]int, len(word1) + 1)
for i := range dp {
dp[i] = make([]int, len(word2) + 1)
dp[i][0] = i
}
for j := range dp[0] {
dp[0][j] = j
}
for i := 1; i < len(dp); i++ {
for j := 1; j < len(dp[0]); j++ {
if word1[i - 1] == word2[j - 1] {
dp[i][j] = dp[i - 1][j - 1]
} else {
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
}
}
}
return dp[len(dp) - 1][len(dp[0]) - 1]
}
No developer has claimed the Ruby throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
It is a question of comparing two strings. After doing similar questions many times, we will develop an intuition to use Dynamic Programming with two-dimensional arrays.
def min_distance(word1, word2)
dp = Array.new(word1.size + 1) do
Array.new(word2.size + 1, 0)
end
dp.each_with_index do |_, i|
dp[i][0] = i
end
dp[0].each_with_index do |_, j|
dp[0][j] = j
end
(1...dp.size).each do |i|
(1...dp[0].size).each do |j|
dp[i][j] =
if word1[i - 1] == word2[j - 1]
dp[i - 1][j - 1]
else
[ dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1] ].min + 1
end
end
end
dp[-1][-1]
end
No developer has claimed the Rust throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #72 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.