Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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};
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/thealgorithms/strings/RemovingStars.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>Example:
* <pre>
* Input: "leet**cod*e"
* Output: "lecoe"
* </pre>
*
* <p>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();
}
}
Loading