/
Edit_Distance.java
64 lines (57 loc) · 1.58 KB
/
Edit_Distance.java
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
// { Driver Code Starts
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
while (T-- > 0) {
String s1 = br.readLine();
String[] S = s1.split(" ");
String s = S[0];
String t = S[1];
Solution ob = new Solution();
int ans = ob.editDistance(s, t);
System.out.println(ans);
}
}
}
// } Driver Code Ends
class Solution {
public int editDistance(String s, String t) {
int a = s.length();
int b = t.length();
char[] arr1 = s.toCharArray();
char[] arr2 = t.toCharArray();
int[][] dp = new int[a + 1][b + 1];
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else if (arr1[i - 1] == arr2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1]));
}
}
}
return dp[a][b];
// Recursive
// return ed(arr1, arr2, a, b);
}
// Recursive
public int ed(char[] arr1, char[] arr2, int a, int b) {
if (a == 0)
return b;
if (b == 0)
return a;
if (arr1[a - 1] == arr2[b - 1]) {
return ed(arr1, arr2, a - 1, b - 1);
} else {
return 1 + Math.min(ed(arr1, arr2, a - 1, b), Math.min(ed(arr1, arr2, a, b - 1), ed(arr1, arr2, a - 1, b - 1)));
}
}
}