Skip to content

Commit 05ec7dd

Browse files
Fix: Implement RemoveStars and ComplexMultiply as per guidelines
1 parent 5f72840 commit 05ec7dd

File tree

4 files changed

+55
-102
lines changed

4 files changed

+55
-102
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* Multiplies two complex numbers given as strings in "a+bi" format.
5+
* Parses the strings manually, performs multiplication, and returns the product.
6+
*/
7+
public class ComplexMultiply {
8+
9+
public static String complexNumberMultiply(String num1, String num2) {
10+
// Extract real and imaginary parts without using parseInt
11+
int plusIndex1 = num1.indexOf('+');
12+
int real1 = Integer.valueOf(num1.substring(0, plusIndex1));
13+
int imag1 = Integer.valueOf(num1.substring(plusIndex1 + 1, num1.length() - 1));
14+
15+
int plusIndex2 = num2.indexOf('+');
16+
int real2 = Integer.valueOf(num2.substring(0, plusIndex2));
17+
int imag2 = Integer.valueOf(num2.substring(plusIndex2 + 1, num2.length() - 1));
18+
19+
// Multiply using complex number formula:
20+
// (a+bi)(c+di) = (ac - bd) + (ad + bc)i
21+
int real = real1 * real2 - imag1 * imag2;
22+
int imaginary = real1 * imag2 + imag1 * real2;
23+
24+
return real + "+" + imaginary + "i";
25+
}
26+
}

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

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* Removes stars from the input string by deleting the closest non-star character
5+
* to the left of each star along with the star itself.
6+
*
7+
* @param s1 The input string containing stars (*)
8+
* @return The string after all stars and their closest left characters are removed
9+
*/
10+
public class RemoveStars {
11+
12+
public static String removeStars(String s1) {
13+
StringBuilder sc = new StringBuilder();
14+
15+
for (int i = 0; i < s1.length(); i++) {
16+
char ch = s1.charAt(i);
17+
18+
if (ch == '*') {
19+
if (sc.length() > 0) {
20+
sc.deleteCharAt(sc.length() - 1);
21+
}
22+
} else {
23+
sc.append(ch);
24+
}
25+
}
26+
27+
return sc.toString();
28+
}
29+
}

src/main/java/com/thealgorithms/strings/Removing stars.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)