-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringRecEdit.java
More file actions
51 lines (39 loc) · 1.16 KB
/
StringRecEdit.java
File metadata and controls
51 lines (39 loc) · 1.16 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
package com.ub.dynamic;
public class StringRecEdit {
static int INSERT = 1;
static int DELETE = 2;
static int MATCH = 0;
private int RecEditDist(String s,String t) {
int[] opt= new int [3];
int lowest_cost;
if (s.length() == 0) {
return (t.length());
}
if (t.length() == 0) {
return (s.length());
}
opt[MATCH] = RecEditDist(s.substring(0,s.length()-1), t.substring(0,t.length() -1))
+ match(s.charAt(s.length() -1 ),t.charAt(t.length()-1));
opt[INSERT] = RecEditDist(s.substring(0,s.length()-1),t) + 1;
opt[DELETE] = RecEditDist( s,t.substring(0,t.length()-1)) + 1;
lowest_cost = opt[MATCH];
for (int i= INSERT;i<=DELETE;i++) {
if (lowest_cost > opt[i]) {
lowest_cost = opt[i];
}
}
return lowest_cost;
}
private int match(char c, char d) {
if(c == d) return 0;
else return 1;
}
public static void main(String args[]) {
String s = "ramayan";
String t = "mahabharat";
StringRecEdit rect = new StringRecEdit();
int no = rect.RecEditDist(s, t);
System.out.println("Edit distnce "+no);
// System.out.println(s.substring(0,s.length() -1));
}
}