forked from exercism/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHamming.java
More file actions
20 lines (16 loc) · 713 Bytes
/
Hamming.java
File metadata and controls
20 lines (16 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Hamming {
public static int compute(String leftStrand, String rightStrand) {
if(leftStrand.length() != rightStrand.length()) {
throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length.");
}
final int length = Math.min(leftStrand.length(), rightStrand.length());
int distance = 0;
for (int i = 0; i < length; i++) {
distance += hammingContributionAt(i, leftStrand, rightStrand);
}
return distance;
}
private static int hammingContributionAt(int index, String leftStrand, String rightStrand) {
return leftStrand.charAt(index) != rightStrand.charAt(index) ? 1 : 0;
}
}