Skip to content

Commit 1be2986

Browse files
Added RemoveStars.java and ComplexNumberMultiplication.java
1 parent 9484c7e commit 1be2986

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.strings;
2+
/**
3+
* @author Swarit Srivastava (https://github.com/SwarritSrivastava)
4+
*/
5+
public final class ComplexNumberMultiplication {
6+
private ComplexNumberMultiplication() {
7+
8+
}
9+
/**
10+
* Multiplies two Complex numbers given as Strings Where,
11+
* real part of each number ranges from [-100,100]
12+
* imaginary part of each number ranges from [-100,100]
13+
* i*i = -1;
14+
* @param num1 The first complex number.
15+
* @param num2 The second complex number.
16+
* @return A string containing the multiplied complex number.
17+
*/
18+
public static String multiplyComplexNumbers(String num1, String num2) {
19+
int i = num1.indexOf('+');
20+
int j = num2.indexOf('+');
21+
String r1 = num1.substring(0, i);
22+
String i1 = num1.substring(i + 1, num1.length() - 1);
23+
String r2 = num2.substring(0, j);
24+
String i2 = num2.substring(j + 1, num2.length() - 1);
25+
int re1 = Integer.parseInt(r1);
26+
int im1 = Integer.parseInt(i1);
27+
int re2 = Integer.parseInt(r2);
28+
int im2 = Integer.parseInt(i2);
29+
int reAns = re1 * re2 - im1 * im2;
30+
int imAns = re1 * im2 + im1 * re2;
31+
return reAns + "+" + imAns + "i";
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.strings;
2+
/**
3+
* @author Swarit Srivastava (https://github.com/SwarritSrivastava)
4+
*/
5+
public final class RemoveStars {
6+
private RemoveStars() {
7+
}
8+
/**
9+
* Removes * characters from the given string. According to the follwing rules
10+
* You are given a string s, which contains stars *.
11+
* In one operation, you can:
12+
* Choose a star in s.
13+
* Remove the closest non-star character to its left, as well as remove the star itself.
14+
* Return the string after all stars have been removed.
15+
* @param input The input string from which duplicate characters need to be removed.
16+
* @return A string containing no stars as per the given constraints.
17+
*/
18+
public static String removeStars(String input) {
19+
if (input == null || input.isEmpty()) {
20+
return input;
21+
}
22+
int n = input.length();
23+
StringBuilder ans = new StringBuilder();
24+
for (int i = 0; i < n; i++) {
25+
char t = input.charAt(i);
26+
if (t != '*') {
27+
ans.append(t);
28+
}
29+
else {
30+
ans.deleteCharAt(ans.length() - 1);
31+
}
32+
}
33+
return ans.toString();
34+
}
35+
}

0 commit comments

Comments
 (0)