Skip to content
Closed
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
26 changes: 26 additions & 0 deletions src/main/java/com/thealgorithms/strings/ComplexMultiply.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.thealgorithms.strings;

/**
* Multiplies two complex numbers given as strings in "a+bi" format.
* Parses the strings manually, performs multiplication, and returns the product.
*/
public class ComplexMultiply {

public static String complexNumberMultiply(String num1, String num2) {
// Extract real and imaginary parts without using parseInt
int plusIndex1 = num1.indexOf('+');
int real1 = Integer.valueOf(num1.substring(0, plusIndex1));
int imag1 = Integer.valueOf(num1.substring(plusIndex1 + 1, num1.length() - 1));

int plusIndex2 = num2.indexOf('+');
int real2 = Integer.valueOf(num2.substring(0, plusIndex2));
int imag2 = Integer.valueOf(num2.substring(plusIndex2 + 1, num2.length() - 1));

// Multiply using complex number formula:
// (a+bi)(c+di) = (ac - bd) + (ad + bc)i
int real = real1 * real2 - imag1 * imag2;
int imaginary = real1 * imag2 + imag1 * real2;

return real + "+" + imaginary + "i";
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/thealgorithms/strings/RemoveStars.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.thealgorithms.strings;

/**
* Removes stars from the input string by deleting the closest non-star character
* to the left of each star along with the star itself.
*
* @param s1 The input string containing stars (*)
* @return The string after all stars and their closest left characters are removed
*/
public class RemoveStars {

public static String removeStars(String s1) {
StringBuilder sc = new StringBuilder();

for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);

if (ch == '*') {
if (sc.length() > 0) {
sc.deleteCharAt(sc.length() - 1);
}
} else {
sc.append(ch);
}
}

return sc.toString();
}
}
Loading