Repository: TheAlgorithms/Java
Pull request #3164 (2022-06-23 02:24:19)

Add Hamming Distance 
### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.



Patch:
diff --git a/src/main/java/com/thealgorithms/strings/HammingDistance.java b/src/main/java/com/thealgorithms/strings/HammingDistance.java
new file mode 100644
index 00000000..e85dca88
--- /dev/null
+++ b/src/main/java/com/thealgorithms/strings/HammingDistance.java
@@ -0,0 +1,32 @@
+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. 
+https://en.wikipedia.org/wiki/Hamming_distance
+*/
+public class HammingDistance {
+
+    /**
+     * calculate the hamming distance between two strings of equal length
+     *
+     * @param s1 the first string
+     * @param s2 the second string
+     * @return {@code int} hamming distance
+     * @throws Exception
+     */
+    public static int calculateHammingDistance(String s1, String s2) throws Exception {
+        if (s1.length() != s2.length()) {
+            throw new Exception("String lengths must be equal");
+        }
+        
+        int stringLength = s1.length();
+        int counter = 0;
+        
+        for (int i = 0; i < stringLength; i++) {
+            if (s1.charAt(i) != s2.charAt(i)) {
+                counter++;
+            }
+        }
+        return counter;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/HammingDistanceTest.java b/src/test/java/com/thealgorithms/strings/HammingDistanceTest.java
new file mode 100644
index 00000000..d17744f1
--- /dev/null
+++ b/src/test/java/com/thealgorithms/strings/HammingDistanceTest.java
@@ -0,0 +1,23 @@
+package com.thealgorithms.strings;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class HammingDistanceTest {
+    @Test
+    void testHammingDistance() throws Exception {
+        assertEquals(HammingDistance.calculateHammingDistance("", ""), 0);
+        assertEquals(HammingDistance.calculateHammingDistance("java", "java"), 0);
+        assertEquals(HammingDistance.calculateHammingDistance("karolin", "kathrin"), 3);
+        assertEquals(HammingDistance.calculateHammingDistance("kathrin", "kerstin"), 4);
+        assertEquals(HammingDistance.calculateHammingDistance("00000", "11111"), 5);
+    }
+
+    @Test
+    void testNotEqualStringLengths() {
+        Exception exception = assertThrows(Exception.class, () -> HammingDistance.calculateHammingDistance("ab", "abc"));
+        assertEquals("String lengths must be equal", exception.getMessage());
+    }
+}