File tree Expand file tree Collapse file tree 2 files changed +82
-0
lines changed
src/main/java/com/thealgorithms/strings Expand file tree Collapse file tree 2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ package main .java .com .thealgorithms .strings ;
2+
3+
4+ public class ComplexNumberMultiplication {
5+ private ComplexNumberMultiplication () {
6+ // Prevent instantiation
7+ }
8+ /**
9+ * Multiplies two complex numbers given as strings.
10+ *
11+ * @param num1 the first complex number in "a+bi" format
12+ * @param num2 the second complex number in "c+di" format
13+ * @return the product of the two complex numbers, also in "x+yi" format
14+ */
15+ public static String multiply (String num1 , String num2 ) {
16+ int [] complex1 = parseComplex (num1 );
17+ int [] complex2 = parseComplex (num2 );
18+
19+ int realPart = complex1 [0 ] * complex2 [0 ] - complex1 [1 ] * complex2 [1 ];
20+ int imaginaryPart = complex1 [0 ] * complex2 [1 ] + complex1 [1 ] * complex2 [0 ];
21+
22+ return realPart + "+" + imaginaryPart + "i" ;
23+ }
24+
25+ /**
26+ * Parses a complex number string into its integer components.
27+ *
28+ * @param complexStr complex number string in "a+bi" format
29+ * @return an array where index 0 is the real part, and index 1 is the imaginary part
30+ */
31+ private static int [] parseComplex (String complexStr ) {
32+ String [] parts = complexStr .split ("\\ +" );
33+ int real = Integer .parseInt (parts [0 ]);
34+ int imaginary = Integer .parseInt (parts [1 ].replace ("i" , "" ));
35+ return new int []{real , imaginary };
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package main .java .com .thealgorithms .strings ;
2+ import java .util .*;
3+ /**
4+ * A utility class to remove stars ('*') from a given string.
5+ * Each '*' deletes the character immediately before it.
6+ *
7+ * <p>Example:
8+ * <pre>
9+ * Input: "leet**cod*e"
10+ * Output: "lecoe"
11+ * </pre>
12+ *
13+ * <p>This implementation uses a stack-like approach for efficient character removal.
14+ * @author Ganesh Mane
15+ */
16+ public final class RemovingStars {
17+ private RemovingStars (){
18+ //prevent instantiation
19+ }
20+
21+ /**
22+ * Removes stars from the given string, simulating backspace behavior.
23+ *
24+ * @param text the input string possibly containing '*'
25+ * @return the final string after removing stars and their preceding characters
26+ */
27+ public static void main (String [] args ) {
28+ String s = "leet**cod*e" ;
29+ System .out .println (removeStarsFromString (s ));
30+ }
31+ public static String removeStarsFromString (String s ){
32+ StringBuilder sb = new StringBuilder ();
33+ for (char ch : s .toCharArray ()){
34+ if (ch == '*' ){
35+ if (sb .length () > 0 ) {
36+ sb .deleteCharAt (sb .length () - 1 );
37+ }
38+ }else {
39+ sb .append (ch );
40+ }
41+ }
42+
43+ return sb .toString ();
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments