File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed
src/main/java/com/thealgorithms/strings Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 1+ package com .thealgorithms .strings ;
2+
3+ /**
4+ * Multiplies two complex numbers represented as strings in the form "a+bi".
5+ *
6+ * Example:
7+ * Input: 1+1i , 1+1i
8+ * Output: 0+2i
9+ */
10+ public final class ComplexNumberMultiply {
11+
12+ private ComplexNumberMultiply () {
13+ }
14+
15+ private static int [] parse (String num ) {
16+ String [] parts = num .split ("\\ +" );
17+ int real = Integer .parseInt (parts [0 ]);
18+ int imaginary = Integer .parseInt (parts [1 ].replace ("i" , "" ));
19+ return new int [] {real , imaginary };
20+ }
21+
22+ public static String multiply (String num1 , String num2 ) {
23+ int [] a = parse (num1 );
24+ int [] b = parse (num2 );
25+
26+ int real = a [0 ] * b [0 ] - a [1 ] * b [1 ];
27+ int imaginary = a [0 ] * b [1 ] + a [1 ] * b [0 ];
28+
29+ return real + "+" + imaginary + "i" ;
30+ }
31+
32+ public static void main (String [] args ) {
33+ System .out .println (multiply ("1+1i" , "1+1i" ));
34+ }
35+ }
Original file line number Diff line number Diff line change 1212public final class RemoveStars {
1313
1414 private RemoveStars () {
15- // Private constructor to prevent instantiation(object creation)
1615 }
1716
1817 public static String removeStars (String s ) {
1918 StringBuilder result = new StringBuilder ();
20-
19+
2120 for (char c : s .toCharArray ()) {
2221 if (c == '*' ) {
2322 if (result .length () > 0 ) {
You can’t perform that action at this time.
0 commit comments