From 1be298662dbd68e65ddd124ee3da394c31c9ce6e Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Mon, 6 Oct 2025 17:01:56 +0530 Subject: [PATCH 01/10] Added RemoveStars.java and ComplexNumberMultiplication.java --- .../strings/ComplexNumberMultiplication.java | 33 +++++++++++++++++ .../thealgorithms/strings/RemoveStars.java | 35 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java create mode 100644 src/main/java/com/thealgorithms/strings/RemoveStars.java 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..d43ce421688d --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java @@ -0,0 +1,33 @@ +package com.thealgorithms.strings; +/** + * @author Swarit Srivastava (https://github.com/SwarritSrivastava) + */ +public final class ComplexNumberMultiplication { + private ComplexNumberMultiplication() { + + } + /** + * Multiplies two Complex numbers given as Strings Where, + * real part of each number ranges from [-100,100] + * imaginary part of each number ranges from [-100,100] + * i*i = -1; + * @param num1 The first complex number. + * @param num2 The second complex number. + * @return A string containing the multiplied complex number. + */ + public static String multiplyComplexNumbers(String num1, String num2) { + int i = num1.indexOf('+'); + int j = num2.indexOf('+'); + String r1 = num1.substring(0, i); + String i1 = num1.substring(i + 1, num1.length() - 1); + String r2 = num2.substring(0, j); + String i2 = num2.substring(j + 1, num2.length() - 1); + int re1 = Integer.parseInt(r1); + int im1 = Integer.parseInt(i1); + int re2 = Integer.parseInt(r2); + int im2 = Integer.parseInt(i2); + int reAns = re1 * re2 - im1 * im2; + int imAns = re1 * im2 + im1 * re2; + return reAns + "+" + imAns + "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..f3d184397d78 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/RemoveStars.java @@ -0,0 +1,35 @@ +package com.thealgorithms.strings; +/** + * @author Swarit Srivastava (https://github.com/SwarritSrivastava) + */ +public final class RemoveStars { + private RemoveStars() { + } + /** + * Removes * characters from the given string. According to the follwing rules + * You are given a string s, which contains stars *. + * In one operation, you can: + * Choose a star in s. + * Remove the closest non-star character to its left, as well as remove the star itself. + * Return the string after all stars have been removed. + * @param input The input string from which duplicate characters need to be removed. + * @return A string containing no stars as per the given constraints. + */ + public static String removeStars(String input) { + if (input == null || input.isEmpty()) { + return input; + } + int n = input.length(); + StringBuilder ans = new StringBuilder(); + for (int i = 0; i < n; i++) { + char t = input.charAt(i); + if (t != '*') { + ans.append(t); + } + else { + ans.deleteCharAt(ans.length() - 1); + } + } + return ans.toString(); + } +} From 5a0c1cf8911346985ea33839dcb5053f43cf0f3d Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Mon, 6 Oct 2025 17:31:31 +0530 Subject: [PATCH 02/10] Added RemoveStarsTest.java and ComplexNumberMultiplicationTest.java --- .../strings/ComplexNumberMultiplication.java | 31 ++++++++++++----- .../thealgorithms/strings/RemoveStars.java | 20 ++++++----- .../ComplexNumberMultiplicationTest.java | 27 +++++++++++++++ .../strings/RemoveStarsTest.java | 33 +++++++++++++++++++ 4 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java create mode 100644 src/test/java/com/thealgorithms/strings/RemoveStarsTest.java diff --git a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java index d43ce421688d..05f9840b8d94 100644 --- a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java +++ b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java @@ -7,27 +7,40 @@ private ComplexNumberMultiplication() { } /** - * Multiplies two Complex numbers given as Strings Where, - * real part of each number ranges from [-100,100] - * imaginary part of each number ranges from [-100,100] - * i*i = -1; - * @param num1 The first complex number. - * @param num2 The second complex number. - * @return A string containing the multiplied complex number. + * 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 i = num1.indexOf('+'); int j = num2.indexOf('+'); + String r1 = num1.substring(0, i); String i1 = num1.substring(i + 1, num1.length() - 1); - String r2 = num2.substring(0, j); - String i2 = num2.substring(j + 1, num2.length() - 1); + int re1 = Integer.parseInt(r1); int im1 = Integer.parseInt(i1); + + String r2 = num2.substring(0, j); + String i2 = num2.substring(j + 1, num2.length() - 1); + int re2 = Integer.parseInt(r2); int im2 = Integer.parseInt(i2); + int reAns = re1 * re2 - im1 * im2; int imAns = re1 * im2 + im1 * re2; + return reAns + "+" + imAns + "i"; } } diff --git a/src/main/java/com/thealgorithms/strings/RemoveStars.java b/src/main/java/com/thealgorithms/strings/RemoveStars.java index f3d184397d78..09491731b90d 100644 --- a/src/main/java/com/thealgorithms/strings/RemoveStars.java +++ b/src/main/java/com/thealgorithms/strings/RemoveStars.java @@ -6,14 +6,18 @@ public final class RemoveStars { private RemoveStars() { } /** - * Removes * characters from the given string. According to the follwing rules - * You are given a string s, which contains stars *. - * In one operation, you can: - * Choose a star in s. - * Remove the closest non-star character to its left, as well as remove the star itself. - * Return the string after all stars have been removed. - * @param input The input string from which duplicate characters need to be removed. - * @return A string containing no stars as per the given constraints. + * 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()) { 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..52c05ae8cc61 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java @@ -0,0 +1,27 @@ +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..aee02cfa8756 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java @@ -0,0 +1,33 @@ +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")); + } +} From 9a6a84cd5aad4cd2052c8e8376248d17a0ceb797 Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Mon, 6 Oct 2025 17:43:36 +0530 Subject: [PATCH 03/10] Refined Variable Names for RemoveStars.java and ComplexNumberMultiplication.java --- .../strings/ComplexNumberMultiplication.java | 26 +++++++++---------- .../thealgorithms/strings/RemoveStars.java | 12 ++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java index 05f9840b8d94..ee65561bdc1f 100644 --- a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java +++ b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java @@ -23,24 +23,24 @@ private ComplexNumberMultiplication() { * @return the resulting complex number after multiplication */ public static String multiplyComplexNumbers(String num1, String num2) { - int i = num1.indexOf('+'); - int j = num2.indexOf('+'); + int plusIndex1 = num1.indexOf('+'); + int plusIndex2 = num2.indexOf('+'); - String r1 = num1.substring(0, i); - String i1 = num1.substring(i + 1, num1.length() - 1); + String realPart1 = num1.substring(0, plusIndex1); + String imagPart1 = num1.substring(plusIndex1 + 1, num1.length() - 1); - int re1 = Integer.parseInt(r1); - int im1 = Integer.parseInt(i1); + int re1 = Integer.parseInt(realPart1); + int im1 = Integer.parseInt(imagPart1); - String r2 = num2.substring(0, j); - String i2 = num2.substring(j + 1, num2.length() - 1); + String realPart2 = num2.substring(0, plusIndex2); + String imagPart2 = num2.substring(plusIndex2 + 1, num2.length() - 1); - int re2 = Integer.parseInt(r2); - int im2 = Integer.parseInt(i2); + int re2 = Integer.parseInt(realPart2); + int im2 = Integer.parseInt(imagPart2); - int reAns = re1 * re2 - im1 * im2; - int imAns = re1 * im2 + im1 * re2; + int reResult = re1 * re2 - im1 * im2; + int imResult = re1 * im2 + im1 * re2; - return reAns + "+" + imAns + "i"; + return reResult + "+" + imResult + "i"; } } diff --git a/src/main/java/com/thealgorithms/strings/RemoveStars.java b/src/main/java/com/thealgorithms/strings/RemoveStars.java index 09491731b90d..c6697a8364b4 100644 --- a/src/main/java/com/thealgorithms/strings/RemoveStars.java +++ b/src/main/java/com/thealgorithms/strings/RemoveStars.java @@ -24,16 +24,16 @@ public static String removeStars(String input) { return input; } int n = input.length(); - StringBuilder ans = new StringBuilder(); + StringBuilder result = new StringBuilder(); for (int i = 0; i < n; i++) { - char t = input.charAt(i); - if (t != '*') { - ans.append(t); + char currentChar = input.charAt(i); + if (currentChar != '*') { + result.append(currentChar); } else { - ans.deleteCharAt(ans.length() - 1); + result.deleteCharAt(result.length() - 1); } } - return ans.toString(); + return result.toString(); } } From 3d7a6eac126dff2f6cf84f59b2b55d5ecdd92d95 Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Mon, 6 Oct 2025 18:03:50 +0530 Subject: [PATCH 04/10] Reformatted code using clang formatter --- .../com/thealgorithms/strings/ComplexNumberMultiplication.java | 1 - src/main/java/com/thealgorithms/strings/RemoveStars.java | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java index ee65561bdc1f..775d7c06562e 100644 --- a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java +++ b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java @@ -4,7 +4,6 @@ */ public final class ComplexNumberMultiplication { private ComplexNumberMultiplication() { - } /** * Multiplies two complex numbers given as strings. diff --git a/src/main/java/com/thealgorithms/strings/RemoveStars.java b/src/main/java/com/thealgorithms/strings/RemoveStars.java index c6697a8364b4..140354098424 100644 --- a/src/main/java/com/thealgorithms/strings/RemoveStars.java +++ b/src/main/java/com/thealgorithms/strings/RemoveStars.java @@ -29,8 +29,7 @@ public static String removeStars(String input) { char currentChar = input.charAt(i); if (currentChar != '*') { result.append(currentChar); - } - else { + } else { result.deleteCharAt(result.length() - 1); } } From 5f2213865835e05b293a158e7a581f8e1f4e2abc Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Mon, 6 Oct 2025 18:05:52 +0530 Subject: [PATCH 05/10] Reformatted code using clang formatter --- .../thealgorithms/strings/ComplexNumberMultiplicationTest.java | 1 + src/test/java/com/thealgorithms/strings/RemoveStarsTest.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java b/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java index 52c05ae8cc61..5cd6fdcbde25 100644 --- a/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java +++ b/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.strings; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; class ComplexNumberMultiplicationTest { diff --git a/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java index aee02cfa8756..be2d2ba35d8a 100644 --- a/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java +++ b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; + import org.junit.jupiter.api.Test; class RemoveStarsTest { From 8420d3bd527067652b87f07aa8c618dfe88a1693 Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Tue, 7 Oct 2025 13:01:18 +0530 Subject: [PATCH 06/10] fix: fixed type conversion error in datastructures/bloomfilter/BloomFilter.java --- .../bloomfilter/BloomFilter.java | 34 ++++++++++++++++++- .../strings/RemoveStarsTest.java | 1 - 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index d60b95110fc2..f9834e80fc50 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,38 @@ 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/test/java/com/thealgorithms/strings/RemoveStarsTest.java b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java index be2d2ba35d8a..aee02cfa8756 100644 --- a/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java +++ b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java @@ -2,7 +2,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; - import org.junit.jupiter.api.Test; class RemoveStarsTest { From c37b8e66932ceb1460fb8a4bf052eaff269b6207 Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Tue, 7 Oct 2025 13:05:31 +0530 Subject: [PATCH 07/10] refactor: Reformatted code usaing clang --- .../bloomfilter/BloomFilter.java | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index f9834e80fc50..aa0f95c256e7 100644 --- a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java +++ b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java @@ -119,32 +119,23 @@ public int compute(T key) { String keyString; if (key instanceof byte[]) { keyString = Arrays.toString((byte[]) key); - } - else if (key instanceof short[]) { + } else if (key instanceof short[]) { keyString = Arrays.toString((short[]) key); - } - else if (key instanceof int[]) { + } else if (key instanceof int[]) { keyString = Arrays.toString((int[]) key); - } - else if (key instanceof long[]) { + } else if (key instanceof long[]) { keyString = Arrays.toString((long[]) key); - } - else if (key instanceof char[]) { + } else if (key instanceof char[]) { keyString = Arrays.toString((char[]) key); - } - else if (key instanceof float[]) { + } else if (key instanceof float[]) { keyString = Arrays.toString((float[]) key); - } - else if (key instanceof double[]) { + } else if (key instanceof double[]) { keyString = Arrays.toString((double[]) key); - } - else if (key instanceof boolean[]) { + } else if (key instanceof boolean[]) { keyString = Arrays.toString((boolean[]) key); - } - else if (key instanceof Object[]) { + } else if (key instanceof Object[]) { keyString = Arrays.deepToString((Object[]) key); - } - else { + } else { keyString = String.valueOf(key); } return index * asciiString(String.valueOf(keyString)); From c495f3dfe2e5089ab3b53f8dc52d82aa27acddb6 Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Tue, 7 Oct 2025 13:07:05 +0530 Subject: [PATCH 08/10] refactor: Reformatted code usaing clang --- src/test/java/com/thealgorithms/strings/RemoveStarsTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java index aee02cfa8756..be2d2ba35d8a 100644 --- a/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java +++ b/src/test/java/com/thealgorithms/strings/RemoveStarsTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; + import org.junit.jupiter.api.Test; class RemoveStarsTest { From f5ec12da50259bfa884add819d64759aba79a4fc Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Mon, 27 Oct 2025 21:05:37 +0530 Subject: [PATCH 09/10] Fixed Multiple If-else statements in BloomFilter.java using reflection api + moved ComplexNumberMultiplication.java to maths package along with its tests --- .../bloomfilter/BloomFilter.java | 35 +++++++++---------- .../ComplexNumberMultiplication.java | 2 +- .../ComplexNumberMultiplicationTest.java | 3 +- 3 files changed, 19 insertions(+), 21 deletions(-) rename src/main/java/com/thealgorithms/{strings => maths}/ComplexNumberMultiplication.java (97%) rename src/test/java/com/thealgorithms/{strings => maths}/ComplexNumberMultiplicationTest.java (95%) diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index aa0f95c256e7..181c806df5b6 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; @@ -117,24 +118,22 @@ private static class Hash { */ public int compute(T 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); + 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); } diff --git a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java b/src/main/java/com/thealgorithms/maths/ComplexNumberMultiplication.java similarity index 97% rename from src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java rename to src/main/java/com/thealgorithms/maths/ComplexNumberMultiplication.java index 775d7c06562e..27be258898ed 100644 --- a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java +++ b/src/main/java/com/thealgorithms/maths/ComplexNumberMultiplication.java @@ -1,4 +1,4 @@ -package com.thealgorithms.strings; +package com.thealgorithms.maths; /** * @author Swarit Srivastava (https://github.com/SwarritSrivastava) */ diff --git a/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java b/src/test/java/com/thealgorithms/maths/ComplexNumberMultiplicationTest.java similarity index 95% rename from src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java rename to src/test/java/com/thealgorithms/maths/ComplexNumberMultiplicationTest.java index 5cd6fdcbde25..568006d78998 100644 --- a/src/test/java/com/thealgorithms/strings/ComplexNumberMultiplicationTest.java +++ b/src/test/java/com/thealgorithms/maths/ComplexNumberMultiplicationTest.java @@ -1,7 +1,6 @@ -package com.thealgorithms.strings; +package com.thealgorithms.maths; import static org.junit.jupiter.api.Assertions.assertEquals; - import org.junit.jupiter.api.Test; class ComplexNumberMultiplicationTest { From 4e8b5d3c5ddbb8f9a8e9b376f1d13b8b64fe52f7 Mon Sep 17 00:00:00 2001 From: SwarritSrivastava Date: Mon, 27 Oct 2025 21:27:37 +0530 Subject: [PATCH 10/10] Fixed Multiple If-else statements in BloomFilter.java using reflection api --- .../bloomfilter/BloomFilter.java | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java index 181c806df5b6..0ca2aab44c45 100644 --- a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java +++ b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java @@ -117,6 +117,30 @@ private static class Hash { * @return the computed hash value */ public int compute(T key) { + return index*contentHash(key); + } + + /** + * Computes the ASCII value sum of the characters in a string. + *

+ * This method iterates through each character of the string and accumulates + * their ASCII values to produce a single integer value. + *

+ * + * @param word the string to compute + * @return the sum of ASCII values of the characters in the string + */ + private int asciiString(String word) { + int sum = 0; + for (char c : word.toCharArray()) { + sum += c; + } + return sum; + } + /** + * Computes a content-based hash for arrays; falls back to ASCII-sum of String value otherwise. + */ + private int contentHash(Object key) { String keyString; if (key != null && key.getClass().isArray()) { if (key instanceof Object[] objects) { @@ -137,25 +161,7 @@ public int compute(T key) { } else { keyString = String.valueOf(key); } - return index * asciiString(String.valueOf(keyString)); - } - - /** - * Computes the ASCII value sum of the characters in a string. - *

- * This method iterates through each character of the string and accumulates - * their ASCII values to produce a single integer value. - *

- * - * @param word the string to compute - * @return the sum of ASCII values of the characters in the string - */ - private int asciiString(String word) { - int sum = 0; - for (char c : word.toCharArray()) { - sum += c; - } - return sum; + return asciiString(String.valueOf(keyString)); } } }