It doesn’t matter to the over all total count, but I’m wondering if there is some inconsistency here
// if s2 is empty, we can remove all the characters of s1 to make it empty too
for (int i1 = 0; i1 <= s1.length(); i1++)
dp[i1][0] = i1;
// if s1 is empty, we have to insert all the characters of s2
for (int i2 = 0; i2 <= s2.length(); i2++)
dp[0][i2] = i2;
The above code makes me feel that the top row is like a an “insert” count when s1 is empty. And the first column is a “remove” count when s2 is empty.
So when I see this code…
dp[i1][i2] = 1 + Math.min(dp[i1 - 1][i2], //delete
Math.min(dp[i1][i2 - 1], //insert
dp[i1 - 1][i2 - 1])); //replace
dp[i1 - 1][i2], <-- is this really delete ?? its accessing towards the first row
dp[i1][i2 - 1] <-- same, is this really insert?? as its accessing towards the first column
I understand these swapping these should impact the result, but just want to understand for consistency.
Thank you