Was asked a version of https://leetcode.com/problems/edit-distance/ but the only difference was that difference of a character meant
deleting and replacing a character.
Added the solution I wrote and the only change made.
int m = word1.length();
int n = word2.length();
if(n*m ==0) return n+m;
if(word1.equals(word2)) return 0;
int [][] d = new int[m+1][n+1];
// init boundaries
for (int i = 0; i < m + 1; i++) {
d[i][0] = i;
}
for (int j = 0; j < n + 1; j++) {
d[0][j] = j;
}
for(int i =1;i<m+1;i++){
for(int j=1;j<n+1;j++){
int left = d[i-1][j]+1;
int below = d[i][j-1]+1;
int diagonal = d[i-1][j-1];
if(word1.charAt(i-1) != word2.charAt(j-1)){
diagonal += 2; // changing this to 2 helped solve it.
}
d[i][j] = Math.min(left,Math.min(below,diagonal));
}
}
return d[m][n];