Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.datastructures.bloomfilter;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.BitSet;

Expand Down Expand Up @@ -127,7 +128,7 @@ private static class Hash<T> {
* @return the computed hash value
*/
public int compute(T key) {
return index * contentHash(key);
return index*contentHash(key);
}

/**
Expand All @@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Each complex number is represented in the form "real+imaginaryi" where:
* <ul>
* <li>real is the real part and is an integer in the range [-100, 100]</li>
* <li>imaginary is the imaginary part and is an integer in the range [-100, 100]</li>
* <li>i * i = -1</li>
* </ul>
*
* 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";
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/thealgorithms/strings/RemoveStars.java
Original file line number Diff line number Diff line change
@@ -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:
* <ul>
* <li>For each star in the string, remove the closest non-star character to its left
* along with the star itself.</li>
* <li>Return the final string after performing all removals.</li>
* <li>Given that such operation is always possible for the input</li>
* </ul>
*
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/thealgorithms/strings/RemoveStarsTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Loading