A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
40
Task/Levenshtein-distance/Java/levenshtein-distance-1.java
Normal file
40
Task/Levenshtein-distance/Java/levenshtein-distance-1.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
public class LevenshteinDistance {
|
||||
|
||||
public static int computeDistance(String s1, String s2) {
|
||||
s1 = s1.toLowerCase();
|
||||
s2 = s2.toLowerCase();
|
||||
|
||||
int[] costs = new int[s2.length() + 1];
|
||||
for (int i = 0; i <= s1.length(); i++) {
|
||||
int lastValue = i;
|
||||
for (int j = 0; j <= s2.length(); j++) {
|
||||
if (i == 0)
|
||||
costs[j] = j;
|
||||
else {
|
||||
if (j > 0) {
|
||||
int newValue = costs[j - 1];
|
||||
if (s1.charAt(i - 1) != s2.charAt(j - 1))
|
||||
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
|
||||
costs[j - 1] = lastValue;
|
||||
lastValue = newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i > 0)
|
||||
costs[s2.length()] = lastValue;
|
||||
}
|
||||
return costs[s2.length()];
|
||||
}
|
||||
|
||||
public static void printDistance(String s1, String s2) {
|
||||
System.out.println(s1 + "-->" + s2 + ": " + computeDistance(s1, s2));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
printDistance("kitten", "sitting");
|
||||
printDistance("rosettacode", "raisethysword");
|
||||
printDistance(new StringBuilder("rosettacode").reverse().toString(), new StringBuilder("raisethysword").reverse().toString());
|
||||
for (int i = 1; i < args.length; i += 2)
|
||||
printDistance(args[i - 1], args[i]);
|
||||
}
|
||||
}
|
||||
46
Task/Levenshtein-distance/Java/levenshtein-distance-2.java
Normal file
46
Task/Levenshtein-distance/Java/levenshtein-distance-2.java
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
public class Levenshtein{
|
||||
public static int levenshtein(String s, String t){
|
||||
/* if either string is empty, difference is inserting all chars
|
||||
* from the other
|
||||
*/
|
||||
if(s.length() == 0) return t.length();
|
||||
if(t.length() == 0) return s.length();
|
||||
|
||||
/* if first letters are the same, the difference is whatever is
|
||||
* required to edit the rest of the strings
|
||||
*/
|
||||
if(s.charAt(0) == t.charAt(0))
|
||||
return levenshtein(s.substring(1), t.substring(1));
|
||||
|
||||
/* else try:
|
||||
* changing first letter of s to that of t,
|
||||
* remove first letter of s, or
|
||||
* remove first letter of t
|
||||
*/
|
||||
int a = levenshtein(s.substring(1), t.substring(1));
|
||||
int b = levenshtein(s, t.substring(1));
|
||||
int c = levenshtein(s.substring(1), t);
|
||||
|
||||
if(a > b) a = b;
|
||||
if(a > c) a = c;
|
||||
|
||||
//any of which is 1 edit plus editing the rest of the strings
|
||||
return a + 1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String s1 = "kitten";
|
||||
String s2 = "sitting";
|
||||
System.out.println("distance between '" + s1 + "' and '"
|
||||
+ s2 + "': " + levenshtein(s1, s2));
|
||||
s1 = "rosettacode";
|
||||
s2 = "raisethysword";
|
||||
System.out.println("distance between '" + s1 + "' and '"
|
||||
+ s2 + "': " + levenshtein(s1, s2));
|
||||
StringBuilder sb1 = new StringBuilder(s1);
|
||||
StringBuilder sb2 = new StringBuilder(s2);
|
||||
System.out.println("distance between '" + sb1.reverse() + "' and '"
|
||||
+ sb2.reverse() + "': "
|
||||
+ levenshtein(sb1.reverse().toString(), sb2.reverse().toString()));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue