Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same.
In one step, you can delete exactly one character in either string.
You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
1 <= word1.length, word2.length <= 500word1 and word2 consist of only lowercase English letters.No developer has claimed the Java throne for LeetCode #583 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 which is about dealing with "two swappable arrays".
After doing similar questions many times, we will form the intuition of using two-dimensional arrays for dynamic programming.
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], dp[i][j - 1]) + 1;
}
}
}
return dp[dp.length - 1][dp[0].length - 1];
}
}
No developer has claimed the C# throne for LeetCode #583 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 which is about dealing with "two swappable arrays".
After doing similar questions many times, we will form the intuition of using two-dimensional arrays for dynamic programming.
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], dp[i, j - 1]) + 1;
}
}
}
return dp[dp.GetUpperBound(0), dp.GetUpperBound(1)];
}
}
No developer has claimed the Python throne for LeetCode #583 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 which is about dealing with "two swappable arrays".
After doing similar questions many times, we will form the intuition of using two-dimensional arrays for dynamic programming.
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], dp[i][j - 1]) + 1
return dp[-1][-1]
No developer has claimed the C++ throne for LeetCode #583 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 which is about dealing with "two swappable arrays".
After doing similar questions many times, we will form the intuition of using two-dimensional arrays for dynamic programming.
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], dp[i][j - 1]) + 1;
}
}
}
return dp[dp.size() - 1][dp[0].size() - 1];
}
};
No developer has claimed the Javascript throne for LeetCode #583 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 which is about dealing with "two swappable arrays".
After doing similar questions many times, we will form the intuition of using two-dimensional arrays for dynamic programming.
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], dp[i][j - 1]) + 1
}
}
}
return dp.at(-1).at(-1)
};
No developer has claimed the Go throne for LeetCode #583 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 which is about dealing with "two swappable arrays".
After doing similar questions many times, we will form the intuition of using two-dimensional arrays for dynamic programming.
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], dp[i][j - 1]) + 1
}
}
}
return dp[len(dp) - 1][len(dp[0]) - 1]
}
No developer has claimed the Ruby throne for LeetCode #583 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 which is about dealing with "two swappable arrays".
After doing similar questions many times, we will form the intuition of using two-dimensional arrays for dynamic programming.
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], dp[i][j - 1] ].min + 1
end
end
end
dp[-1][-1]
end
No developer has claimed the Rust throne for LeetCode #583 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 #583 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 #583 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.