583. Delete Operation for Two Strings
Solution
請參考 How to solve DP - String? Template and 4 steps to be followed.
public int minDistance(String word1, String word2) {
char[] s1 = word1.toCharArray();
char[] s2 = word2.toCharArray();
int[][] dp = new int[s1.length+1][s2.length+1];
for(int i = 0; i <= s1.length; i++){
dp[i][0] = i;
}
for(int j = 0; j <= s2.length; j++){
dp[0][j] = j;
}
for(int i = 1; i <= s1.length; i++){
for(int j = 1; j <= s2.length; j++){
if(s1[i-1] != s2[j-1]){
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + 1;
}
else{
dp[i][j] = dp[i-1][j-1];
}
}
}
return dp[s1.length][s2.length];
}
Last updated
Was this helpful?