Skip to content

Commit a4b33c7

Browse files
committed
Add RemoveStars and ComplexNumberMultiply string algorithms
1 parent 91c8a73 commit a4b33c7

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* Multiplies two complex numbers represented as strings in the form "a+bi".
5+
*
6+
* Example:
7+
* Input: 1+1i , 1+1i
8+
* Output: 0+2i
9+
*/
10+
public final class ComplexNumberMultiply {
11+
12+
private ComplexNumberMultiply() {
13+
}
14+
15+
private static int[] parse(String num) {
16+
String[] parts = num.split("\\+");
17+
int real = Integer.parseInt(parts[0]);
18+
int imaginary = Integer.parseInt(parts[1].replace("i", ""));
19+
return new int[] {real, imaginary};
20+
}
21+
22+
public static String multiply(String num1, String num2) {
23+
int[] a = parse(num1);
24+
int[] b = parse(num2);
25+
26+
int real = a[0] * b[0] - a[1] * b[1];
27+
int imaginary = a[0] * b[1] + a[1] * b[0];
28+
29+
return real + "+" + imaginary + "i";
30+
}
31+
32+
public static void main(String[] args) {
33+
System.out.println(multiply("1+1i", "1+1i"));
34+
}
35+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
public final class RemoveStars {
1313

1414
private RemoveStars() {
15-
// Private constructor to prevent instantiation(object creation)
1615
}
1716

1817
public static String removeStars(String s) {
1918
StringBuilder result = new StringBuilder();
20-
19+
2120
for (char c : s.toCharArray()) {
2221
if (c == '*') {
2322
if (result.length() > 0) {

0 commit comments

Comments
 (0)