forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHammingDistance.java
More file actions
50 lines (43 loc) · 1.64 KB
/
Copy pathHammingDistance.java
File metadata and controls
50 lines (43 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Calculate the number of positions at which the corresponding symbols are different.
*
* @author Atom
* @see <a href="https://en.wikipedia.org/wiki/Hamming_distance">Hamming distance</a>
*/
public class HammingDistance {
public static int hammingDistance(String s1, String s2) {
if (s1.length() != s2.length()) throw new IllegalArgumentException("The two strings must be the same length.");
int distance = 0;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) { distance++; }
}
return distance;
}
public static int hammingDistanceIgnoreCase(String s1, String s2) {
return hammingDistance(s1.toLowerCase(), s2.toLowerCase());
}
public static int hammingDistance(final int x, final int y) {
return Integer.bitCount(x ^ y);
}
public static int hammingDistance(final long x, final long y) {
return Long.bitCount(x ^ y);
}
public static void main(String[] args) {
System.out.println(hammingDistance("five", "five"));
System.out.println(hammingDistance("five", "four"));
System.out.println(hammingDistance("five", "FIVE"));
System.out.println(hammingDistanceIgnoreCase("five", "FIVE"));
System.out.println();
System.out.println(hammingDistance(1, 1));
System.out.println(hammingDistance(1, 2));
System.out.println(hammingDistance(1, 3));
System.out.println(hammingDistance(1, 4));
System.out.println(hammingDistance(1, 5));
System.out.println();
System.out.println(hammingDistance(1L, 1L));
System.out.println(hammingDistance(1L, 2L));
System.out.println(hammingDistance(1L, 3L));
System.out.println(hammingDistance(1L, 4L));
System.out.println(hammingDistance(1L, 5L));
}
}