Skip to content

Commit 6f67a4b

Browse files
committed
fix:add more codes to the string folder
1 parent a0b6c52 commit 6f67a4b

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* Algorithm to multiply two complex numbers represented as strings.
5+
*
6+
* A complex number can be represented as a string on the form "real+imaginaryi" where:
7+
* - real is the real part and is an integer in the range [-100, 100]
8+
* - imaginary is the imaginary part and is an integer in the range [-100, 100]
9+
* - i² == -1
10+
*
11+
* Given two complex numbers num1 and num2 as strings, this returns a string
12+
* of the complex number that represents their multiplication.
13+
*
14+
* Mathematical formula:
15+
* (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
16+
*
17+
* Example:
18+
* Input: num1 = "1+1i", num2 = "1+1i"
19+
* Output: "0+2i"
20+
* Explanation: (1 + i) * (1 + i) = 1 + i + i + i² = 1 + 2i - 1 = 0 + 2i
21+
*
22+
* @see <a href="https://leetcode.com/problems/complex-number-multiplication/">LeetCode Problem</a>
23+
*/
24+
public final class ComplexNumberMultiplication {
25+
private ComplexNumberMultiplication() {
26+
}
27+
28+
/**
29+
* Multiplies two complex numbers represented as strings.
30+
*
31+
* @param num1 The first complex number in the format "a+bi"
32+
* @param num2 The second complex number in the format "c+di"
33+
* @return The product of the two complex numbers in the format "real+imaginaryi"
34+
*/
35+
public static String complexNumberMultiply(String num1, String num2) {
36+
if (num1 == null || num2 == null || num1.isEmpty() || num2.isEmpty()) {
37+
return "0+0i";
38+
}
39+
40+
// Parse the first complex number
41+
int[] complex1 = parseComplexNumber(num1);
42+
int a = complex1[0]; // Real part of num1
43+
int b = complex1[1]; // Imaginary part of num1
44+
45+
// Parse the second complex number
46+
int[] complex2 = parseComplexNumber(num2);
47+
int c = complex2[0]; // Real part of num2
48+
int d = complex2[1]; // Imaginary part of num2
49+
50+
// Calculate the multiplication using the formula: (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
51+
int realPart = a * c - b * d;
52+
int imaginaryPart = a * d + b * c;
53+
54+
return realPart + "+" + imaginaryPart + "i";
55+
}
56+
57+
/**
58+
* Parses a complex number string into its real and imaginary components.
59+
*
60+
* @param num The complex number string in the format "a+bi" or "a-bi"
61+
* @return An array where index 0 is the real part and index 1 is the imaginary part
62+
*/
63+
private static int[] parseComplexNumber(String num) {
64+
// Find the position of '+' or '-' after the first character (to handle negative real parts)
65+
int plusIndex = num.indexOf('+', 1);
66+
int minusIndex = num.indexOf('-', 1);
67+
68+
// Determine the separator position
69+
int separatorIndex = (plusIndex != -1) ? plusIndex : minusIndex;
70+
71+
// Extract real and imaginary parts
72+
int real = Integer.parseInt(num.substring(0, separatorIndex));
73+
// Remove the 'i' at the end and parse the imaginary part
74+
int imaginary = Integer.parseInt(num.substring(separatorIndex + 1, num.length() - 1));
75+
76+
return new int[] {real, imaginary};
77+
}
78+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* Algorithm to remove stars from a string along with the closest non-star character to the left.
5+
*
6+
* You are given a string s, which contains stars *.
7+
* In one operation, you can:
8+
* - Choose a star in s.
9+
* - Remove the closest non-star character to its left, as well as remove the star itself.
10+
*
11+
* Example:
12+
* Input: s = "leet**cod*e"
13+
* Output: "lecoe"
14+
* Explanation:
15+
* - We remove the first star and the 't' to its left, resulting in "lee*cod*e"
16+
* - We remove the second star and the 'e' to its left, resulting in "lecod*e"
17+
* - We remove the third star and the 'd' to its left, resulting in "lecoe"
18+
*
19+
* @see <a href="https://leetcode.com/problems/removing-stars-from-a-string/">LeetCode Problem</a>
20+
*/
21+
public final class RemovingStarsFromString {
22+
private RemovingStarsFromString() {
23+
}
24+
25+
/**
26+
* Removes all stars from the string along with their closest non-star character to the left.
27+
*
28+
* @param s The input string containing stars
29+
* @return The resulting string after all stars have been removed
30+
*/
31+
public static String removeStars(String s) {
32+
if (s == null || s.isEmpty()) {
33+
return s;
34+
}
35+
36+
StringBuilder result = new StringBuilder();
37+
38+
for (char c : s.toCharArray()) {
39+
if (c == '*') {
40+
// Remove the last character (closest non-star character to the left)
41+
if (result.length() > 0) {
42+
result.deleteCharAt(result.length() - 1);
43+
}
44+
} else {
45+
result.append(c);
46+
}
47+
}
48+
49+
return result.toString();
50+
}
51+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
class ComplexNumberMultiplicationTest {
7+
8+
@Test
9+
void testBasicMultiplication() {
10+
assertEquals("0+2i", ComplexNumberMultiplication.complexNumberMultiply("1+1i", "1+1i"));
11+
}
12+
13+
@Test
14+
void testMultiplicationWithZero() {
15+
assertEquals("0+0i", ComplexNumberMultiplication.complexNumberMultiply("0+0i", "1+1i"));
16+
}
17+
18+
@Test
19+
void testRealNumbersOnly() {
20+
assertEquals("6+0i", ComplexNumberMultiplication.complexNumberMultiply("2+0i", "3+0i"));
21+
}
22+
23+
@Test
24+
void testImaginaryNumbersOnly() {
25+
assertEquals("-6+0i", ComplexNumberMultiplication.complexNumberMultiply("0+2i", "0+3i"));
26+
}
27+
28+
@Test
29+
void testNegativeRealPart() {
30+
assertEquals("-3+4i", ComplexNumberMultiplication.complexNumberMultiply("1+1i", "-1+3i"));
31+
}
32+
33+
@Test
34+
void testNegativeImaginaryPart() {
35+
assertEquals("0+-2i", ComplexNumberMultiplication.complexNumberMultiply("1-1i", "1-1i"));
36+
}
37+
38+
@Test
39+
void testLargerNumbers() {
40+
assertEquals("0+20i", ComplexNumberMultiplication.complexNumberMultiply("2+3i", "4+2i"));
41+
}
42+
43+
@Test
44+
void testNegativeResult() {
45+
assertEquals("-7+22i", ComplexNumberMultiplication.complexNumberMultiply("3+4i", "1+6i"));
46+
}
47+
48+
@Test
49+
void testIdentityMultiplication() {
50+
assertEquals("5+3i", ComplexNumberMultiplication.complexNumberMultiply("5+3i", "1+0i"));
51+
}
52+
53+
@Test
54+
void testNullInput() {
55+
assertEquals("0+0i", ComplexNumberMultiplication.complexNumberMultiply(null, "1+1i"));
56+
}
57+
58+
@Test
59+
void testEmptyString() {
60+
assertEquals("0+0i", ComplexNumberMultiplication.complexNumberMultiply("", "1+1i"));
61+
}
62+
63+
@Test
64+
void testBothNegativeParts() {
65+
assertEquals("0+-2i", ComplexNumberMultiplication.complexNumberMultiply("-1+-1i", "1+0i"));
66+
}
67+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
class RemovingStarsFromStringTest {
7+
8+
@Test
9+
void testEmptyString() {
10+
assertEquals("", RemovingStarsFromString.removeStars(""));
11+
}
12+
13+
@Test
14+
void testNullString() {
15+
assertEquals(null, RemovingStarsFromString.removeStars(null));
16+
}
17+
18+
@Test
19+
void testStringWithNoStars() {
20+
assertEquals("leetcode", RemovingStarsFromString.removeStars("leetcode"));
21+
}
22+
23+
@Test
24+
void testBasicExample() {
25+
assertEquals("lecoe", RemovingStarsFromString.removeStars("leet**cod*e"));
26+
}
27+
28+
@Test
29+
void testAllStars() {
30+
assertEquals("", RemovingStarsFromString.removeStars("abc***"));
31+
}
32+
33+
@Test
34+
void testSingleCharacterWithStar() {
35+
assertEquals("", RemovingStarsFromString.removeStars("a*"));
36+
}
37+
38+
@Test
39+
void testMultipleConsecutiveStars() {
40+
assertEquals("ac", RemovingStarsFromString.removeStars("abc**d*"));
41+
}
42+
43+
@Test
44+
void testStarAtEnd() {
45+
assertEquals("leet", RemovingStarsFromString.removeStars("leetc*"));
46+
}
47+
48+
@Test
49+
void testComplexPattern() {
50+
assertEquals("", RemovingStarsFromString.removeStars("a*b*c*"));
51+
}
52+
53+
@Test
54+
void testNoRemoval() {
55+
assertEquals("hello", RemovingStarsFromString.removeStars("hello"));
56+
}
57+
}

0 commit comments

Comments
 (0)