[InterviewBit] Edit Distance

Edit Distance

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
int Solution::minDistance(string A, string B) {
int dp[A.length() + 1][B.length() + 1];
for(int i = 0; i <= A.length(); i++) dp[i][0] = i;
for(int i = 0; i <= B.length(); i++) dp[0][i] = i;
for(int i = 1; i <= A.length(); i++) {
for(int j = 1; j <= B.length(); j++) {
dp[i][j] = A[i-1] == B[j-1] ? dp[i-1][j-1] : 1 + min({dp[i-1][j-1], dp[i-1][j], dp[i][j-1]});
}
}
return dp[A.length()][B.length()];
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/11/PS/interviewbit/edit-distance/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.