diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index 90625ad1c902..d9686344a3c3 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.lang.reflect.Array; import java.util.Arrays; import java.util.BitSet; @@ -127,7 +128,7 @@ private static class Hash { * @return the computed hash value */ public int compute(T key) { - return index * contentHash(key); + return index*contentHash(key); } /** @@ -147,31 +148,31 @@ private int asciiString(String word) { } return sum; } - /** * Computes a content-based hash for arrays; falls back to ASCII-sum of String value otherwise. */ private int contentHash(Object key) { - if (key instanceof int[]) { - return Arrays.hashCode((int[]) key); - } else if (key instanceof long[]) { - return Arrays.hashCode((long[]) key); - } else if (key instanceof byte[]) { - return Arrays.hashCode((byte[]) key); - } else if (key instanceof short[]) { - return Arrays.hashCode((short[]) key); - } else if (key instanceof char[]) { - return Arrays.hashCode((char[]) key); - } else if (key instanceof boolean[]) { - return Arrays.hashCode((boolean[]) key); - } else if (key instanceof float[]) { - return Arrays.hashCode((float[]) key); - } else if (key instanceof double[]) { - return Arrays.hashCode((double[]) key); - } else if (key instanceof Object[]) { - return Arrays.deepHashCode((Object[]) key); + String keyString; + if (key != null && key.getClass().isArray()) { + if (key instanceof Object[] objects) { + keyString = Arrays.deepToString(objects); + } + else { + int length = Array.getLength(key); + StringBuilder sb = new StringBuilder("["); + for (int i = 0; i < length; i++) { + sb.append(Array.get(key, i)); + if (i < length - 1) { + sb.append(", "); + } + } + sb.append("]"); + keyString = sb.toString(); + } + } else { + keyString = String.valueOf(key); } - return asciiString(String.valueOf(key)); + return asciiString(String.valueOf(keyString)); } } } diff --git a/src/main/java/com/thealgorithms/maths/ComplexNumberMultiplication.java b/src/main/java/com/thealgorithms/maths/ComplexNumberMultiplication.java new file mode 100644 index 000000000000..27be258898ed --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/ComplexNumberMultiplication.java @@ -0,0 +1,45 @@ +package com.thealgorithms.maths; +/** + * @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/maths/ComplexNumberMultiplicationTest.java b/src/test/java/com/thealgorithms/maths/ComplexNumberMultiplicationTest.java new file mode 100644 index 000000000000..568006d78998 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/ComplexNumberMultiplicationTest.java @@ -0,0 +1,27 @@ +package com.thealgorithms.maths; + +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")); + } +}