Skip to content

Commit 5a0c1cf

Browse files
Added RemoveStarsTest.java and ComplexNumberMultiplicationTest.java
1 parent 1be2986 commit 5a0c1cf

File tree

4 files changed

+94
-17
lines changed

4 files changed

+94
-17
lines changed

src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,40 @@ private ComplexNumberMultiplication() {
77

88
}
99
/**
10-
* Multiplies two Complex numbers given as Strings Where,
11-
* real part of each number ranges from [-100,100]
12-
* imaginary part of each number ranges from [-100,100]
13-
* i*i = -1;
14-
* @param num1 The first complex number.
15-
* @param num2 The second complex number.
16-
* @return A string containing the multiplied complex number.
10+
* Multiplies two complex numbers given as strings.
11+
* <p>
12+
* Each complex number is represented in the form "real+imaginaryi" where:
13+
* <ul>
14+
* <li>real is the real part and is an integer in the range [-100, 100]</li>
15+
* <li>imaginary is the imaginary part and is an integer in the range [-100, 100]</li>
16+
* <li>i * i = -1</li>
17+
* </ul>
18+
*
19+
* Example: {@code multiplyComplexNumbers("1+1i", "1+1i") -> "0+2i"}
20+
*
21+
* @param num1 the first complex number
22+
* @param num2 the second complex number
23+
* @return the resulting complex number after multiplication
1724
*/
1825
public static String multiplyComplexNumbers(String num1, String num2) {
1926
int i = num1.indexOf('+');
2027
int j = num2.indexOf('+');
28+
2129
String r1 = num1.substring(0, i);
2230
String i1 = num1.substring(i + 1, num1.length() - 1);
23-
String r2 = num2.substring(0, j);
24-
String i2 = num2.substring(j + 1, num2.length() - 1);
31+
2532
int re1 = Integer.parseInt(r1);
2633
int im1 = Integer.parseInt(i1);
34+
35+
String r2 = num2.substring(0, j);
36+
String i2 = num2.substring(j + 1, num2.length() - 1);
37+
2738
int re2 = Integer.parseInt(r2);
2839
int im2 = Integer.parseInt(i2);
40+
2941
int reAns = re1 * re2 - im1 * im2;
3042
int imAns = re1 * im2 + im1 * re2;
43+
3144
return reAns + "+" + imAns + "i";
3245
}
3346
}

src/main/java/com/thealgorithms/strings/RemoveStars.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ public final class RemoveStars {
66
private RemoveStars() {
77
}
88
/**
9-
* Removes * characters from the given string. According to the follwing rules
10-
* You are given a string s, which contains stars *.
11-
* In one operation, you can:
12-
* Choose a star in s.
13-
* Remove the closest non-star character to its left, as well as remove the star itself.
14-
* Return the string after all stars have been removed.
15-
* @param input The input string from which duplicate characters need to be removed.
16-
* @return A string containing no stars as per the given constraints.
9+
* Removes stars ('*') from the given string according to the following rules:
10+
* <ul>
11+
* <li>For each star in the string, remove the closest non-star character to its left
12+
* along with the star itself.</li>
13+
* <li>Return the final string after performing all removals.</li>
14+
* <li>Given that such operation is always possible for the input</li>
15+
* </ul>
16+
*
17+
* Example: {@code "leet**cod*e" -> "lecoe"}
18+
*
19+
* @param input The input string possibly containing '*' characters.
20+
* @return The resulting string after removing stars as per the rules.
1721
*/
1822
public static String removeStars(String input) {
1923
if (input == null || input.isEmpty()) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
class ComplexNumberMultiplicationTest {
7+
8+
@Test
9+
void testExample() {
10+
assertEquals("0+2i", ComplexNumberMultiplication.multiplyComplexNumbers("1+1i", "1+1i"));
11+
}
12+
13+
@Test
14+
void testNegative() {
15+
assertEquals("2+0i", ComplexNumberMultiplication.multiplyComplexNumbers("1+1i", "1+-1i"));
16+
}
17+
18+
@Test
19+
void testZero() {
20+
assertEquals("0+0i", ComplexNumberMultiplication.multiplyComplexNumbers("0+0i", "5+5i"));
21+
}
22+
23+
@Test
24+
void testDifferentValues() {
25+
assertEquals("5+5i", ComplexNumberMultiplication.multiplyComplexNumbers("1+2i", "3+-1i"));
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import org.junit.jupiter.api.Test;
6+
7+
class RemoveStarsTest {
8+
9+
@Test
10+
void testExample() {
11+
assertEquals("lecoe", RemoveStars.removeStars("leet**cod*e"));
12+
}
13+
14+
@Test
15+
void testMultipleStars() {
16+
assertEquals("c", RemoveStars.removeStars("ab*c*d**c"));
17+
}
18+
19+
@Test
20+
void testEmptyInput() {
21+
assertEquals("", RemoveStars.removeStars(""));
22+
}
23+
24+
@Test
25+
void testNullInput() {
26+
assertNull(RemoveStars.removeStars(null));
27+
}
28+
29+
@Test
30+
void testNoStars() {
31+
assertEquals("hello", RemoveStars.removeStars("hello"));
32+
}
33+
}

0 commit comments

Comments
 (0)