Skip to content

Commit c9bda3d

Browse files
Add string algorithms: RemoveStars and ComplexNumberMultiply (#7275)
* Add RemoveStars and ComplexNumberMultiply string algorithms * Add RemoveStars and ComplexNumberMultiply string algorithms * Add unit tests for RemoveStars and ComplexNumber Multiply * Fix checkstyle * Remove redundant main method * Move ComplexNumberMultiply to maths package and add input validation with tests * Apply spotless formatting --------- Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
1 parent 1646eda commit c9bda3d

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* Multiplies two complex numbers represented as strings in the form "a+bi".
5+
* Supports negative values and validates input format.
6+
*/
7+
public final class ComplexNumberMultiply {
8+
9+
private ComplexNumberMultiply() {
10+
}
11+
12+
private static int[] parse(String num) {
13+
if (num == null || !num.matches("-?\\d+\\+-?\\d+i")) {
14+
throw new IllegalArgumentException("Invalid complex number format: " + num);
15+
}
16+
17+
String[] parts = num.split("\\+");
18+
int real = Integer.parseInt(parts[0]);
19+
int imaginary = Integer.parseInt(parts[1].replace("i", ""));
20+
return new int[] {real, imaginary};
21+
}
22+
23+
public static String multiply(String num1, String num2) {
24+
int[] a = parse(num1);
25+
int[] b = parse(num2);
26+
27+
int real = a[0] * b[0] - a[1] * b[1];
28+
int imaginary = a[0] * b[1] + a[1] * b[0];
29+
30+
return real + "+" + imaginary + "i";
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* Removes characters affected by '*' in a string.
5+
* Each '*' deletes the closest non-star character to its left.
6+
*
7+
* Example:
8+
* Input: leet**cod*e
9+
* Output: lecoe
10+
*/
11+
12+
public final class RemoveStars {
13+
14+
private RemoveStars() {
15+
}
16+
17+
public static String removeStars(String s) {
18+
StringBuilder result = new StringBuilder();
19+
20+
for (char c : s.toCharArray()) {
21+
if (c == '*') {
22+
if (result.length() > 0) {
23+
result.deleteCharAt(result.length() - 1);
24+
}
25+
} else {
26+
result.append(c);
27+
}
28+
}
29+
return result.toString();
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class ComplexNumberMultiplyTest {
9+
10+
@Test
11+
void testExample() {
12+
assertEquals("0+2i", ComplexNumberMultiply.multiply("1+1i", "1+1i"));
13+
}
14+
15+
@Test
16+
void testNegative() {
17+
assertEquals("0+-2i", ComplexNumberMultiply.multiply("1+-1i", "1+-1i"));
18+
}
19+
20+
@Test
21+
void testZero() {
22+
assertEquals("0+0i", ComplexNumberMultiply.multiply("0+0i", "5+3i"));
23+
}
24+
25+
@Test
26+
void testInvalidFormat() {
27+
assertThrows(IllegalArgumentException.class, () -> ComplexNumberMultiply.multiply("1+1", "1+1i"));
28+
}
29+
30+
@Test
31+
void testNullInput() {
32+
assertThrows(IllegalArgumentException.class, () -> ComplexNumberMultiply.multiply(null, "1+1i"));
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class RemoveStarsTest {
8+
9+
@Test
10+
void testExampleCase() {
11+
assertEquals("lecoe", RemoveStars.removeStars("leet**cod*e"));
12+
}
13+
14+
@Test
15+
void testAllStars() {
16+
assertEquals("", RemoveStars.removeStars("abc***"));
17+
}
18+
19+
@Test
20+
void testNoStars() {
21+
assertEquals("hello", RemoveStars.removeStars("hello"));
22+
}
23+
24+
@Test
25+
void testSingleCharacter() {
26+
assertEquals("", RemoveStars.removeStars("a*"));
27+
}
28+
}

0 commit comments

Comments
 (0)