-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_14.java
More file actions
37 lines (31 loc) · 1.02 KB
/
Problem_14.java
File metadata and controls
37 lines (31 loc) · 1.02 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
package strings;
import java.util.*;
// Problem Title => EDIT Distance [Very Imp]
public class Problem_14 {
static int min(int x, int y, int z){
if (x <= y && x <= z)
return x;
if (y <= x && y <= z)
return y;
else
return z;
}
static int editDistance(String str1, String str2, int m, int n){
if(m == 0) return n;
if(n == 0) return n;
if(str1.charAt(m - 1) == str2.charAt(n - 1))
return editDistance(str1, str2, m - 1, n - 1);
return 1
+ min(editDistance(str1, str2, m, n - 1), // Insert
editDistance(str1, str2, m - 1, n), // Remove
editDistance(str1, str2, m - 1, n - 1) // Replace
);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
sc.close();
System.out.println(editDistance(str1, str2, str1.length(), str2.length()));
}
}