forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LevenshteinDistance.java
95 lines (76 loc) · 3.47 KB
/
LevenshteinDistance.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package dynamicprogramming;
public class LevenshteinDistance {
/*
* Compute Levenshtein distance between two strings.
* Given two strings, compute the Levenshtein distance between them i.e.
* the minimum number of edits required to convert one string into the other.
*
* For example, the Levenshtein distance between "kitten" and "sitting" is 3.
* The minimum steps required to transform the former into latter are:
* kitten → sitten (substitution of "s" for "k")
* sitten → sittin (substitution of "i" for "e")
* sittin → sitting (insertion of "g" at the end)
*
* Runtime Complexity:
* Quadratic, O(n2)
*
* Memory Complexity:
* Quadratic, O(n2)
*
* Levenshtein distance 'LD' is a measure of the difference between two strings s1 and s2.
* It is the minimum number of deletions, insertions, or substitutions required to transform s1 into s2.
*
* if s1 is empty, return length of s2
* if s2 is empty, return length of s1
* if last characters of s1 and s2 match, initialize 'cost' to 0, otherwise initialize 'cost' to 1
* recursively compute 'd1' [(Levenshtein distance of s1 - 1 and s2) + 1 ]
* recursively compute 'd2' [(Levenshtein distance of s1 and s2 - 1) + 1]
* recursively compute 'd3' [(Levenshtein distance of s1 - 1 and s2 - 1) + cost]
* return minimum of d1, d2, d3
* */
private static int minimum(int a, int b, int c) {
return Math.min(Math.min(a, b), c);
}
public static int computeLevenshteinDistance(String str1,
String str2) {
if (str1 == str2) return 0;
if (str1.length() == 0)
return str2.length();
if (str2.length() == 0)
return str1.length();
int[][] d = new int[str1.length() + 1][str2.length() + 1];
for (int i = 0; i <= str1.length(); i++) {
d[i][0] = i;
}
for (int j = 1; j <= str2.length(); j++) {
d[0][j] = j;
}
int cost;
for (int i = 1; i <= str1.length(); i++) {
for (int j = 1; j <= str2.length(); j++) {
if (str1.charAt(i - 1) == str2.charAt(j - 1))
cost = 0;
else
cost = 1;
d[i][j] = minimum(
d[i - 1][j] + 1,
d[i][j - 1] + 1,
d[i - 1][j - 1] + cost);
}
}
return d[str1.length()][str2.length()];
}
public static void main(String[] args) {
System.out.println(computeLevenshteinDistance("", ""));
System.out.println(computeLevenshteinDistance("kite", ""));
System.out.println(computeLevenshteinDistance("", "kitten"));
System.out.println(computeLevenshteinDistance("computer", "computer"));
System.out.println(computeLevenshteinDistance("test", "text"));
System.out.println(computeLevenshteinDistance("kitten", "sitting"));
System.out.println(computeLevenshteinDistance("this is a string", "This is a string"));
System.out.println(computeLevenshteinDistance("this is a string", "thisisastring"));
System.out.println(computeLevenshteinDistance("appropriate meaning", "approximate matching"));
System.out.println(computeLevenshteinDistance("intention", "execution"));
System.out.println(computeLevenshteinDistance("min", "max"));
}
}