diff --git a/src/main/java/com/thealgorithms/strings/ComplexMultiply.java b/src/main/java/com/thealgorithms/strings/ComplexMultiply.java new file mode 100644 index 000000000000..6a2922ef4419 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/ComplexMultiply.java @@ -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"; + } +} diff --git a/src/main/java/com/thealgorithms/strings/RemoveStars.java b/src/main/java/com/thealgorithms/strings/RemoveStars.java new file mode 100644 index 000000000000..521d41a2887b --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/RemoveStars.java @@ -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(); + } +}