In this problem, I need to get the Hamming distance (the Hamming distance between two lines of equal length is the number of positions at which the corresponding characters differ - from Wikipedia) between two sequences of sequence 1 and sequence2.
At first, I made 2 new lines, which are 2 source lines, but with a reduced case, to simplify the comparison. Then I resorted to using a for loop and if we compare 2 lines. For any differences in characters in these two pairs of lines, the loop will add 1 to int x = 0. The method return 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 the code looks good and quite functional? Anything I could fix or optimize the code? Thank you in advance. I'm a huge noob, so forgive me if I ask for something stupid
source share