diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index d60b95110fc2..aa0f95c256e7 100644 --- a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java +++ b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java @@ -1,5 +1,6 @@ package com.thealgorithms.datastructures.bloomfilter; +import java.util.Arrays; import java.util.BitSet; /** @@ -115,7 +116,29 @@ private static class Hash { * @return the computed hash value */ public int compute(T key) { - return index * asciiString(String.valueOf(key)); + String keyString; + if (key instanceof byte[]) { + keyString = Arrays.toString((byte[]) key); + } else if (key instanceof short[]) { + keyString = Arrays.toString((short[]) key); + } else if (key instanceof int[]) { + keyString = Arrays.toString((int[]) key); + } else if (key instanceof long[]) { + keyString = Arrays.toString((long[]) key); + } else if (key instanceof char[]) { + keyString = Arrays.toString((char[]) key); + } else if (key instanceof float[]) { + keyString = Arrays.toString((float[]) key); + } else if (key instanceof double[]) { + keyString = Arrays.toString((double[]) key); + } else if (key instanceof boolean[]) { + keyString = Arrays.toString((boolean[]) key); + } else if (key instanceof Object[]) { + keyString = Arrays.deepToString((Object[]) key); + } else { + keyString = String.valueOf(key); + } + return index * asciiString(String.valueOf(keyString)); } /** diff --git a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java new file mode 100644 index 000000000000..775d7c06562e --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java @@ -0,0 +1,45 @@ +package com.thealgorithms.strings; +/** + * @author Swarit Srivastava (https://github.com/SwarritSrivastava) + */ +public final class ComplexNumberMultiplication { + private ComplexNumberMultiplication() { + } + /** + * Multiplies two complex numbers given as strings. + *

+ * Each complex number is represented in the form "real+imaginaryi" where: + *

+ * + * Example: {@code multiplyComplexNumbers("1+1i", "1+1i") -> "0+2i"} + * + * @param num1 the first complex number + * @param num2 the second complex number + * @return the resulting complex number after multiplication + */ + public static String multiplyComplexNumbers(String num1, String num2) { + int plusIndex1 = num1.indexOf('+'); + int plusIndex2 = num2.indexOf('+'); + + String realPart1 = num1.substring(0, plusIndex1); + String imagPart1 = num1.substring(plusIndex1 + 1, num1.length() - 1); + + int re1 = Integer.parseInt(realPart1); + int im1 = Integer.parseInt(imagPart1); + + String realPart2 = num2.substring(0, plusIndex2); + String imagPart2 = num2.substring(plusIndex2 + 1, num2.length() - 1); + + int re2 = Integer.parseInt(realPart2); + int im2 = Integer.parseInt(imagPart2); + + int reResult = re1 * re2 - im1 * im2; + int imResult = re1 * im2 + im1 * re2; + + return reResult + "+" + imResult + "i"; + } +} diff --git a/src/main/java/com/thealgorithms/strings/RemoveStars.java b/src/main/java/com/thealgorithms/strings/RemoveStars.java new file mode 100644 index 000000000000..140354098424 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/RemoveStars.java @@ -0,0 +1,38 @@ +package com.thealgorithms.strings; +/** + * @author Swarit Srivastava (https://github.com/SwarritSrivastava) + */ +public final class RemoveStars { + private RemoveStars() { + } + /** + * Removes stars ('*') from the given string according to the following rules: + * + * + * Example: {@code "leet**cod*e" -> "lecoe"} + * + * @param input The input string possibly containing '*' characters. + * @return The resulting string after removing stars as per the rules. + */ + public static String removeStars(String input) { + if (input == null || input.isEmpty()) { + return input; + } + int n = input.length(); + StringBuilder result = new StringBuilder(); + for (int i = 0; i < n; i++) { + char currentChar = input.charAt(i); + if (currentChar != '*') { + result.append(currentChar); + } else { + result.deleteCharAt(result.length() - 1); + } + } + return result.toString(); + } +} diff --git a/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java b/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java new file mode 100644 index 000000000000..5cd6fdcbde25 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java @@ -0,0 +1,28 @@ +package com.thealgorithms.strings; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class ComplexNumberMultiplicationTest { + + @Test + void testExample() { + assertEquals("0+2i", ComplexNumberMultiplication.multiplyComplexNumbers("1+1i", "1+1i")); + } + + @Test + void testNegative() { + assertEquals("2+0i", ComplexNumberMultiplication.multiplyComplexNumbers("1+1i", "1+-1i")); + } + + @Test + void testZero() { + assertEquals("0+0i", ComplexNumberMultiplication.multiplyComplexNumbers("0+0i", "5+5i")); + } + + @Test + void testDifferentValues() { + assertEquals("5+5i", ComplexNumberMultiplication.multiplyComplexNumbers("1+2i", "3+-1i")); + } +} diff --git a/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java new file mode 100644 index 000000000000..be2d2ba35d8a --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java @@ -0,0 +1,34 @@ +package com.thealgorithms.strings; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +class RemoveStarsTest { + + @Test + void testExample() { + assertEquals("lecoe", RemoveStars.removeStars("leet**cod*e")); + } + + @Test + void testMultipleStars() { + assertEquals("c", RemoveStars.removeStars("ab*c*d**c")); + } + + @Test + void testEmptyInput() { + assertEquals("", RemoveStars.removeStars("")); + } + + @Test + void testNullInput() { + assertNull(RemoveStars.removeStars(null)); + } + + @Test + void testNoStars() { + assertEquals("hello", RemoveStars.removeStars("hello")); + } +}