Most frequent k chars distance: Difference between revisions

Content added Content deleted
Line 197: Line 197:


=={{header|Java}}==
=={{header|Java}}==
{{incorrect|Java|This will fail catastrophically on “<tt>ABACADAEAFAEADACABA</tt>” as that has 10 ‘<tt>A</tt>’ characters in it.}}
{{incomplete|Java| Works now when there more than 10 occurences of a character in string. Please check, there can be more elegant way to deal with it. Comment was : This will fail catastrophically on “<tt>ABACADAEAFAEADACABA</tt>” as that has 10 ‘<tt>A</tt>’ characters in it.}}
Translation of the pseudo-code of the Wikipedia article [[wp:Most frequent k characters]] to [[wp:java]] implementation of three functions given in the definition section are given below with [[wp:JavaDoc]] comments:
Translation of the pseudo-code of the Wikipedia article [[wp:Most frequent k characters]] to [[wp:java]] implementation of three functions given in the definition section are given below with [[wp:JavaDoc]] comments:


Line 280: Line 280:
public static int getDiff(String str1, String str2, int limit) {
public static int getDiff(String str1, String str2, int limit) {
int similarity = 0;
int similarity = 0;
int k = 0;
for (int i = 0; i < str1.length(); i += 2) {
for (int i = 0; i < str1.length() ; i = k) {
System.out.println(i);
k ++;
int pos = str2.indexOf(str1.charAt(i));
System.out.println(str2.charAt(i) + " - " + pos);
if (Character.isLetter(str1.charAt(i))) {
int pos = str2.indexOf(str1.charAt(i));
if (pos >= 0) {
similarity += Integer.parseInt(str2.substring(pos+1, pos+2)) + Character.getNumericValue(str1.charAt(i+1));
if (pos >= 0) {
}
String digitStr1 = "";
while ( k < str1.length() && !Character.isLetter(str1.charAt(k))) {
}
digitStr1 += str1.charAt(k);
k++;
return limit-similarity;
}
int k2 = pos+1;
String digitStr2 = "";
while (k2 < str2.length() && !Character.isLetter(str2.charAt(k2)) ) {
digitStr2 += str2.charAt(k2);
k2++;
}
similarity += Integer.parseInt(digitStr2)
+ Integer.parseInt(digitStr1);
}
}
}
return Math.abs(limit - similarity);
}
}
/**
/**