Skip to content
Open
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.util.Arrays;
import java.util.BitSet;

/**
Expand Down Expand Up @@ -115,7 +116,29 @@ private static class Hash<T> {
* @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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <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,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"));
}
}
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"));
}
}