-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathEditDistance.cpp
More file actions
67 lines (49 loc) · 1.32 KB
/
Copy pathEditDistance.cpp
File metadata and controls
67 lines (49 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Given two strings A and B,
Find the minimum number of steps required to convert A to B. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
Insert a character
Delete a character
Replace a character
Input Format:
The first argument of input contains a string, A.
The second argument of input contains a string, B.
Output Format:
Return an integer, representing the minimum number of steps required.
Constraints:
1 <= length(A), length(B) <= 450
Examples:
Input 1:
A = "abad"
B = "abac"
Output 1:
1
Explanation 1:
Operation 1: Replace d with c.
Input 2:
A = "Anshuman"
B = "Antihuman"
Output 2:
2
Explanation 2:
=> Operation 1: Replace s with t.
=> Operation 2: Insert i.
https://www.interviewbit.com/problems/edit-distance/
*/
int Solution::minDistance(string A, string B) {
int m = A.size(), n = B.size();
int sol[m+1][n+1];
for(auto i=0; i<=m; ++i){
for(auto j=0; j<=n; ++j){
if( i == 0 )
sol[i][j] = j;
else if( j == 0 )
sol[i][j] = i;
else if( A[i-1] == B[j-1] )
sol[i][j] = sol[i-1][j-1];
else
sol[i][j] = 1 + min( sol[i][j-1], min( sol[i-1][j], sol[i-1][j-1]));
}
}
return sol[m][n];
}