refactor: HammingDistance
(#5404)
* refactor: HammingDistance * checkstyle: fix formatting --------- Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
parent
d810a1d4da
commit
c5b73ec742
@ -1,34 +1,45 @@
|
|||||||
package com.thealgorithms.strings;
|
package com.thealgorithms.strings;
|
||||||
|
|
||||||
/* In information theory, the Hamming distance between two strings of equal length
|
/**
|
||||||
is the number of positions at which the corresponding symbols are different.
|
* Class for calculating the Hamming distance between two strings of equal length.
|
||||||
https://en.wikipedia.org/wiki/Hamming_distance
|
* <p>
|
||||||
*/
|
* The Hamming distance is the number of positions at which the corresponding symbols are different.
|
||||||
|
* It is used in information theory, coding theory, and computer science.
|
||||||
|
* </p>
|
||||||
|
* @see <a href="https://en.wikipedia.org/wiki/Hamming_distance">Hamming distance - Wikipedia</a>
|
||||||
|
*/
|
||||||
public final class HammingDistance {
|
public final class HammingDistance {
|
||||||
private HammingDistance() {
|
private HammingDistance() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* calculate the hamming distance between two strings of equal length
|
* Calculates the Hamming distance between two strings of equal length.
|
||||||
|
* <p>
|
||||||
|
* The Hamming distance is defined only for strings of equal length. If the strings are not
|
||||||
|
* of equal length, this method throws an {@code IllegalArgumentException}.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @param s1 the first string
|
* @param s1 the first string
|
||||||
* @param s2 the second string
|
* @param s2 the second string
|
||||||
* @return {@code int} hamming distance
|
* @return the Hamming distance between the two strings
|
||||||
* @throws Exception
|
* @throws IllegalArgumentException if the lengths of {@code s1} and {@code s2} are not equal
|
||||||
*/
|
*/
|
||||||
public static int calculateHammingDistance(String s1, String s2) throws Exception {
|
public static int calculateHammingDistance(String s1, String s2) {
|
||||||
if (s1.length() != s2.length()) {
|
if (s1 == null || s2 == null) {
|
||||||
throw new Exception("String lengths must be equal");
|
throw new IllegalArgumentException("Strings must not be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
int stringLength = s1.length();
|
if (s1.length() != s2.length()) {
|
||||||
int counter = 0;
|
throw new IllegalArgumentException("String lengths must be equal");
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < stringLength; i++) {
|
int distance = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < s1.length(); i++) {
|
||||||
if (s1.charAt(i) != s2.charAt(i)) {
|
if (s1.charAt(i) != s2.charAt(i)) {
|
||||||
counter++;
|
distance++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return counter;
|
return distance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,22 +3,34 @@ package com.thealgorithms.strings;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
public class HammingDistanceTest {
|
class HammingDistanceTest {
|
||||||
|
|
||||||
@Test
|
@ParameterizedTest
|
||||||
void testHammingDistance() throws Exception {
|
@CsvSource({"'', '', 0", "'java', 'java', 0", "'karolin', 'kathrin', 3", "'kathrin', 'kerstin', 4", "'00000', '11111', 5", "'10101', '10100', 1"})
|
||||||
assertEquals(HammingDistance.calculateHammingDistance("", ""), 0);
|
void testHammingDistance(String s1, String s2, int expected) {
|
||||||
assertEquals(HammingDistance.calculateHammingDistance("java", "java"), 0);
|
assertEquals(expected, HammingDistance.calculateHammingDistance(s1, s2));
|
||||||
assertEquals(HammingDistance.calculateHammingDistance("karolin", "kathrin"), 3);
|
}
|
||||||
assertEquals(HammingDistance.calculateHammingDistance("kathrin", "kerstin"), 4);
|
|
||||||
assertEquals(HammingDistance.calculateHammingDistance("00000", "11111"), 5);
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideNullInputs")
|
||||||
|
void testHammingDistanceWithNullInputs(String input1, String input2) {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> HammingDistance.calculateHammingDistance(input1, input2));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> provideNullInputs() {
|
||||||
|
return Stream.of(Arguments.of(null, "abc"), Arguments.of("abc", null), Arguments.of(null, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNotEqualStringLengths() {
|
void testNotEqualStringLengths() {
|
||||||
Exception exception = assertThrows(Exception.class, () -> HammingDistance.calculateHammingDistance("ab", "abc"));
|
Exception exception = assertThrows(IllegalArgumentException.class, () -> HammingDistance.calculateHammingDistance("ab", "abc"));
|
||||||
assertEquals("String lengths must be equal", exception.getMessage());
|
assertEquals("String lengths must be equal", exception.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user