diff --git a/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java new file mode 100644 index 000000000000..81b90a6f2326 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/ComplexNumberMultiplication.java @@ -0,0 +1,37 @@ +package main.java.com.thealgorithms.strings; + + +public class ComplexNumberMultiplication { + private ComplexNumberMultiplication() { + // Prevent instantiation + } + /** + * Multiplies two complex numbers given as strings. + * + * @param num1 the first complex number in "a+bi" format + * @param num2 the second complex number in "c+di" format + * @return the product of the two complex numbers, also in "x+yi" format + */ + public static String multiply(String num1, String num2) { + int[] complex1 = parseComplex(num1); + int[] complex2 = parseComplex(num2); + + int realPart = complex1[0] * complex2[0] - complex1[1] * complex2[1]; + int imaginaryPart = complex1[0] * complex2[1] + complex1[1] * complex2[0]; + + return realPart + "+" + imaginaryPart + "i"; + } + + /** + * Parses a complex number string into its integer components. + * + * @param complexStr complex number string in "a+bi" format + * @return an array where index 0 is the real part, and index 1 is the imaginary part + */ + private static int[] parseComplex(String complexStr) { + String[] parts = complexStr.split("\\+"); + int real = Integer.parseInt(parts[0]); + int imaginary = Integer.parseInt(parts[1].replace("i", "")); + return new int[]{real, imaginary}; + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/RemovingStars.java b/src/main/java/com/thealgorithms/strings/RemovingStars.java new file mode 100644 index 000000000000..d7d4d8848bd8 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/RemovingStars.java @@ -0,0 +1,45 @@ +package main.java.com.thealgorithms.strings; +import java.util.*; +/** + * A utility class to remove stars ('*') from a given string. + * Each '*' deletes the character immediately before it. + * + *

Example: + *

+ * Input:  "leet**cod*e"
+ * Output: "lecoe"
+ * 
+ * + *

This implementation uses a stack-like approach for efficient character removal. + * @author Ganesh Mane + */ +public final class RemovingStars{ + private RemovingStars(){ + //prevent instantiation + } + + /** + * Removes stars from the given string, simulating backspace behavior. + * + * @param text the input string possibly containing '*' + * @return the final string after removing stars and their preceding characters + */ + public static void main(String[] args) { + String s = "leet**cod*e"; + System.out.println(removeStarsFromString(s)); + } + public static String removeStarsFromString(String s){ + StringBuilder sb = new StringBuilder(); + for(char ch : s.toCharArray()){ + if(ch == '*'){ + if (sb.length() > 0) { + sb.deleteCharAt(sb.length() - 1); + } + }else{ + sb.append(ch); + } + } + + return sb.toString(); + } +} \ No newline at end of file