Using for loop to get the Hamming distance between 2 strings

强颜欢笑 提交于 2019-12-08 21:28:37

问题


In this task i need to get the Hamming distance (the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different - from Wikipedia) between the two strings sequence1 and sequence2.

First i made 2 new strings which is the 2 original strings but both with lowered case to make comparing easier. Then i resorted to using the for loop and if to compare the 2 strings. For any differences in characters in these 2 pair of string, the loop would add 1 to an int x = 0. The returns of the method will be the value of this x.

public static int getHammingDistance(String sequence1, String sequence2) {
    int a = 0;
    String sequenceX = sequence1.toLowerCase();
    String sequenceY = sequence2.toLowerCase();
    for (int x = 0; x < sequenceX.length(); x++) {
        for (int y = 0; y < sequenceY.length(); y++) {
            if (sequenceX.charAt(x) == sequenceY.charAt(y)) {
                a += 0;
            } else if (sequenceX.charAt(x) != sequenceY.charAt(y)) {
                a += 1;
            }
        }
    }
    return a;
}

So does the code looks good and functional enough? Anything i could to fix or to optimize the code? Thanks in advance. I'm a huge noob so pardon me if i asked anything silly


回答1:


From my point the following implementation would be ok:

public static int getHammingDistance(String sequence1, String sequence2) {
    char[] s1 = sequence1.toCharArray();
    char[] s2 = sequence2.toCharArray();

    int shorter = Math.min(s1.length, s2.length);
    int longest = Math.max(s1.length, s2.length);

    int result = 0;
    for (int i=0; i<shorter; i++) {
        if (s1[i] != s2[i]) result++;
    }

    result += longest - shorter;

    return result;
}
  1. uses array, what avoids the invocation of two method (charAt) for each single char that needs to be compared;
  2. avoid exception when one string is longer than the other.



回答2:


your code is completely off. as you said yourself, the distance is the number of places where the strings differ - so you should only have 1 loop, going over both strings at once. instead you have 2 nested loops that compare every index in string a to every index in string b.

also, writing an if condition that results in a+=0 is a waste of time.

try this instead:

for (int x = 0; x < sequenceX.length(); x++) { //both are of the same length
    if (sequenceX.charAt(x) != sequenceY.charAt(x)) {
        a += 1;
    }
}

also, this is still a naive approach which will probbaly not work with complex unicode characters (where 2 characters can be logically equal yet not have the same character code)




回答3:


public static int getHammingDistance(String sequenceX, String sequenceY) {
    int a = 0;
   // String sequenceX = sequence1.toLowerCase();
    //String sequenceY = sequence2.toLowerCase();
    if (sequenceX.length() != sequenceY.length()) {
        return -1; //input strings should be of equal length
    }

    for (int i = 0; i < sequenceX.length(); i++) {
        if (sequenceX.charAt(i) != sequenceY.charAt(i)) {
            a++;
        }
    }
    return a;
}



回答4:


Your code is OK, however I'd suggest you the following improvements.

  1. do not use charAt() of string. Get char array from string using toCharArray() before loop and then work with this array. This is more readable and more effective.
  2. The structure

        if (sequenceX.charAt(x) == sequenceY.charAt(y)) {
            a += 0;
        } else if (sequenceX.charAt(x) != sequenceY.charAt(y)) {
            a += 1;
        }
    

    looks redundant. Fix it to: if (sequenceX.charAt(x) == sequenceY.charAt(y)) { a += 0; } else { a += 1; }

Moreover taking into account that I recommended you to work with array change it to something like:

a += seqx[x] == seqY[x] ? 0 : 1

less code less bugs...

EDIT: as mentionded by @radai you do not need if/else structure at all: adding 0 to a is redundant.



来源:https://stackoverflow.com/questions/16260752/using-for-loop-to-get-the-hamming-distance-between-2-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!