-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditDistance.java
More file actions
30 lines (29 loc) · 940 Bytes
/
EditDistance.java
File metadata and controls
30 lines (29 loc) · 940 Bytes
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
import java.io.*;
import java.util.*;
class EditDistance{
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String t = br.readLine();
int[][] dp = new int[t.length() + 1][s.length() + 1];
for(int j = 0; j <= s.length(); j++) {
dp[0][j] = j;
}
for(int i = 0; i <= t.length(); i++) {
dp[i][0] = i;
}
for(int i = 1; i <= t.length(); i++) {
for(int j = 1; j <= s.length(); j++) {
char c1 = s.charAt(j-1);
char c2 = t.charAt(i-1);
if(c1 == c2) {
dp[i][j] = dp[i-1][j-1];
} else {
dp[i][j] = Math.min(dp[i-1][j], Math.min(dp[i][j-1], dp[i-1][j-1])) + 1;
}
}
}
System.out.println(dp[t.length()][s.length()]);
}
}