Skip to content

Commit c5b73ec

Browse files
alxkmalxkm
and
alxkm
authored
refactor: HammingDistance (#5404)
* refactor: HammingDistance * checkstyle: fix formatting --------- Co-authored-by: alxkm <[email protected]>
1 parent d810a1d commit c5b73ec

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
11
package com.thealgorithms.strings;
22

3-
/* In information theory, the Hamming distance between two strings of equal length
4-
is the number of positions at which the corresponding symbols are different.
5-
https://en.wikipedia.org/wiki/Hamming_distance
6-
*/
3+
/**
4+
* Class for calculating the Hamming distance between two strings of equal length.
5+
* <p>
6+
* The Hamming distance is the number of positions at which the corresponding symbols are different.
7+
* It is used in information theory, coding theory, and computer science.
8+
* </p>
9+
* @see <a href="https://en.wikipedia.org/wiki/Hamming_distance">Hamming distance - Wikipedia</a>
10+
*/
711
public final class HammingDistance {
812
private HammingDistance() {
913
}
1014

1115
/**
12-
* calculate the hamming distance between two strings of equal length
16+
* Calculates the Hamming distance between two strings of equal length.
17+
* <p>
18+
* The Hamming distance is defined only for strings of equal length. If the strings are not
19+
* of equal length, this method throws an {@code IllegalArgumentException}.
20+
* </p>
1321
*
1422
* @param s1 the first string
1523
* @param s2 the second string
16-
* @return {@code int} hamming distance
17-
* @throws Exception
24+
* @return the Hamming distance between the two strings
25+
* @throws IllegalArgumentException if the lengths of {@code s1} and {@code s2} are not equal
1826
*/
19-
public static int calculateHammingDistance(String s1, String s2) throws Exception {
27+
public static int calculateHammingDistance(String s1, String s2) {
28+
if (s1 == null || s2 == null) {
29+
throw new IllegalArgumentException("Strings must not be null");
30+
}
31+
2032
if (s1.length() != s2.length()) {
21-
throw new Exception("String lengths must be equal");
33+
throw new IllegalArgumentException("String lengths must be equal");
2234
}
2335

24-
int stringLength = s1.length();
25-
int counter = 0;
36+
int distance = 0;
2637

27-
for (int i = 0; i < stringLength; i++) {
38+
for (int i = 0; i < s1.length(); i++) {
2839
if (s1.charAt(i) != s2.charAt(i)) {
29-
counter++;
40+
distance++;
3041
}
3142
}
32-
return counter;
43+
return distance;
3344
}
3445
}

src/test/java/com/thealgorithms/strings/HammingDistanceTest.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,34 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6+
import java.util.stream.Stream;
67
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.CsvSource;
11+
import org.junit.jupiter.params.provider.MethodSource;
712

8-
public class HammingDistanceTest {
13+
class HammingDistanceTest {
914

10-
@Test
11-
void testHammingDistance() throws Exception {
12-
assertEquals(HammingDistance.calculateHammingDistance("", ""), 0);
13-
assertEquals(HammingDistance.calculateHammingDistance("java", "java"), 0);
14-
assertEquals(HammingDistance.calculateHammingDistance("karolin", "kathrin"), 3);
15-
assertEquals(HammingDistance.calculateHammingDistance("kathrin", "kerstin"), 4);
16-
assertEquals(HammingDistance.calculateHammingDistance("00000", "11111"), 5);
15+
@ParameterizedTest
16+
@CsvSource({"'', '', 0", "'java', 'java', 0", "'karolin', 'kathrin', 3", "'kathrin', 'kerstin', 4", "'00000', '11111', 5", "'10101', '10100', 1"})
17+
void testHammingDistance(String s1, String s2, int expected) {
18+
assertEquals(expected, HammingDistance.calculateHammingDistance(s1, s2));
19+
}
20+
21+
@ParameterizedTest
22+
@MethodSource("provideNullInputs")
23+
void testHammingDistanceWithNullInputs(String input1, String input2) {
24+
assertThrows(IllegalArgumentException.class, () -> HammingDistance.calculateHammingDistance(input1, input2));
25+
}
26+
27+
private static Stream<Arguments> provideNullInputs() {
28+
return Stream.of(Arguments.of(null, "abc"), Arguments.of("abc", null), Arguments.of(null, null));
1729
}
1830

1931
@Test
2032
void testNotEqualStringLengths() {
21-
Exception exception = assertThrows(Exception.class, () -> HammingDistance.calculateHammingDistance("ab", "abc"));
33+
Exception exception = assertThrows(IllegalArgumentException.class, () -> HammingDistance.calculateHammingDistance("ab", "abc"));
2234
assertEquals("String lengths must be equal", exception.getMessage());
2335
}
2436
}

0 commit comments

Comments
 (0)