java中统计两个字符串的不匹配率要用到的方法

来源:百度知道 编辑:UC知道 时间:2024/06/08 05:25:11
从别处听说的题目。我自己没有找到合适的方法。大约是在长度相等的情况下统计。如果没有现成的方法,自己写的方法也可以告诉我。

/** 假设位置相同且字符完全相同时才认为是匹配 */

public class StringMatchRate {

public static float getMatchRate(String s1, String s2) {
if(s1 == null || s2 == null) {
return 0.0f;
}
int len1 = s1.length();
int len2 = s2.length();
int len = Math.min(len1, len2);
int eqCount = 0;
for(int i = 0; i < len; i++) {
if(s1.charAt(i) == s2.charAt(i)) {
eqCount ++;
}
}
return ((float)eqCount) / Math.max(len1, len2);
}

public static float getUnmatchRate(String s1, String s2) {

return (1.0f - getMatchRate(s1, s2));
}

public static void main(String[] args) {
System.out.println(getMatchRate("abcd23", "abcdef"));
System.out.println(getUnmatchRate("abcd23", "abcdef"));
}
}