We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符 删除一个字符 替换一个字符
Input: word1 = "horse", word2 = "ros" Output: 3 Explanation: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e')
Input: word1 = "intention", word2 = "execution" Output: 5 Explanation: 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')
The text was updated successfully, but these errors were encountered:
/** * @param {string} word1 * @param {string} word2 * @return {number} */ var minDistance = function(word1, word2) { const dp = Array.from(new Array(word1.length + 1), () => new Array(word2.length + 1)); for (let i = 0; i <= word1.length; i++) { for (let j = 0; j <= word2.length; j++) { if (i === 0 || j === 0) { dp[i][j] = i | j; } else if (word1[i - 1] === word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1; } } } return dp[word1.length][word2.length]; };
var minDistance = function(word1: string, word2: string): number { const dp: number[][] = Array.from(new Array(word1.length + 1), () => new Array(word2.length + 1)); for (let i = 0; i <= word1.length; i++) { for (let j = 0; j <= word2.length; j++) { if (i === 0 || j === 0) { dp[i][j] = i | j; } else if (word1[i - 1] === word2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1; } } } return dp[word1.length][word2.length]; };
Sorry, something went wrong.
No branches or pull requests
72. Edit Distance
给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
Example 1
Example 2
The text was updated successfully, but these errors were encountered: