-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDistance.java
More file actions
75 lines (57 loc) · 2.37 KB
/
EditDistance.java
File metadata and controls
75 lines (57 loc) · 2.37 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
68
69
70
71
72
73
74
75
import java.util.HashMap;
import java.util.Scanner;
public class EditDistance {
public static void main(String[] args){
// String s11 = " SERIOUS";
String s12 = "I have to go for pee pee and we are rocking the chair whole heartedly";
String[] arr = s12.split(" ", 20);
HashMap<String, Integer> array = new HashMap<String, Integer>();
for(String s : arr){
if(array.containsKey(s)){
array.put(s, array.get(s)+1);
}else{
array.put(s,1);
}
}
Scanner scan = new Scanner(System.in);
String s11 = scan.nextLine();
editDistance(s11, array);
}
public static void editDistance(String s11, HashMap<String, Integer> array){
for(HashMap.Entry<String, Integer> str : array.entrySet()){
String s12 = str.getKey();
int E[][] = new int[s11.length() + 1][s12.length() + 1];
for(int i = 0; i <= s11.length(); i++){
E[i][0] = i;
}
for(int j = 0; j <= s12.length(); j++){
E[0][j] = j;
}
for(int i = 1; i <= s11.length(); i++){
for(int j = 1; j <= s12.length(); j++){
if(s11.charAt(i-1) != s12.charAt(j-1)){
if(E[i][j-1] <= E[i-1][j] && E[i][j-1] <= E[i-1][j-1]){
E[i][j] = 1 + E[i][j-1];
}else if(E[i-1][j] <= E[i][j-1] && E[i-1][j] <= E[i-1][j-1]){
E[i][j] = 1 + E[i-1][j];
}else if(E[i-1][j-1] <= E[i][j-1] && E[i-1][j-1] <= E[i-1][j]){
E[i][j] = 1 + E[i-1][j-1];
}
}else if(s11.charAt(i-1) == s12.charAt(j-1)){
if( E[i][j-1] <= E[i-1][j] && E[i][j-1] <= E[i-1][j-1]){
E[i][j] = 0 + E[i][j-1];
}else if(E[i-1][j] <= E[i][j-1] && E[i-1][j] <= E[i-1][j-1]){
E[i][j] = 0 + E[i-1][j];
}else if(E[i-1][j-1] <= E[i][j-1] && E[i-1][j-1] <= E[i-1][j]){
E[i][j] = 0 + E[i-1][j-1];
}
}
}
}
if((E[s11.length()][s12.length()]/s11.length())*100 <= 20){
System.out.println(s12);
System.out.println(E[s11.length()][s12.length()]);
}
}
}
}