-
Notifications
You must be signed in to change notification settings - Fork 21k
Add string algorithms: RemoveStars and ComplexNumberMultiply #7275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DenizAltunkapan
merged 8 commits into
TheAlgorithms:master
from
SYEDMDSAAD:add-string-algorithms
Feb 18, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91c8a73
Add RemoveStars and ComplexNumberMultiply string algorithms
SYEDMDSAAD a4b33c7
Add RemoveStars and ComplexNumberMultiply string algorithms
SYEDMDSAAD a743920
Add unit tests for RemoveStars and ComplexNumber Multiply
SYEDMDSAAD 2278690
Fix checkstyle
SYEDMDSAAD e2a3cdb
Remove redundant main method
SYEDMDSAAD baadaab
Move ComplexNumberMultiply to maths package and add input validation …
SYEDMDSAAD eae49a2
Apply spotless formatting
SYEDMDSAAD c8d3895
Merge branch 'master' into add-string-algorithms
DenizAltunkapan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/com/thealgorithms/maths/ComplexNumberMultiply.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.thealgorithms.maths; | ||
|
|
||
| /** | ||
| * Multiplies two complex numbers represented as strings in the form "a+bi". | ||
| * Supports negative values and validates input format. | ||
| */ | ||
| public final class ComplexNumberMultiply { | ||
|
|
||
| private ComplexNumberMultiply() { | ||
| } | ||
|
|
||
| private static int[] parse(String num) { | ||
| if (num == null || !num.matches("-?\\d+\\+-?\\d+i")) { | ||
| throw new IllegalArgumentException("Invalid complex number format: " + num); | ||
| } | ||
|
|
||
| String[] parts = num.split("\\+"); | ||
| int real = Integer.parseInt(parts[0]); | ||
| int imaginary = Integer.parseInt(parts[1].replace("i", "")); | ||
| return new int[] {real, imaginary}; | ||
| } | ||
|
|
||
| public static String multiply(String num1, String num2) { | ||
| int[] a = parse(num1); | ||
| int[] b = parse(num2); | ||
|
|
||
| int real = a[0] * b[0] - a[1] * b[1]; | ||
| int imaginary = a[0] * b[1] + a[1] * b[0]; | ||
|
|
||
| return real + "+" + imaginary + "i"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.thealgorithms.strings; | ||
|
|
||
| /** | ||
| * Removes characters affected by '*' in a string. | ||
| * Each '*' deletes the closest non-star character to its left. | ||
| * | ||
| * Example: | ||
| * Input: leet**cod*e | ||
| * Output: lecoe | ||
| */ | ||
|
|
||
| public final class RemoveStars { | ||
|
|
||
| private RemoveStars() { | ||
| } | ||
|
|
||
| public static String removeStars(String s) { | ||
| StringBuilder result = new StringBuilder(); | ||
|
|
||
| for (char c : s.toCharArray()) { | ||
| if (c == '*') { | ||
| if (result.length() > 0) { | ||
| result.deleteCharAt(result.length() - 1); | ||
| } | ||
| } else { | ||
| result.append(c); | ||
| } | ||
| } | ||
| return result.toString(); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/test/java/com/thealgorithms/maths/ComplexNumberMultiplyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.thealgorithms.maths; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ComplexNumberMultiplyTest { | ||
|
|
||
| @Test | ||
| void testExample() { | ||
| assertEquals("0+2i", ComplexNumberMultiply.multiply("1+1i", "1+1i")); | ||
| } | ||
|
|
||
| @Test | ||
| void testNegative() { | ||
| assertEquals("0+-2i", ComplexNumberMultiply.multiply("1+-1i", "1+-1i")); | ||
| } | ||
|
|
||
| @Test | ||
| void testZero() { | ||
| assertEquals("0+0i", ComplexNumberMultiply.multiply("0+0i", "5+3i")); | ||
| } | ||
|
|
||
| @Test | ||
| void testInvalidFormat() { | ||
| assertThrows(IllegalArgumentException.class, () -> ComplexNumberMultiply.multiply("1+1", "1+1i")); | ||
| } | ||
|
|
||
| @Test | ||
| void testNullInput() { | ||
| assertThrows(IllegalArgumentException.class, () -> ComplexNumberMultiply.multiply(null, "1+1i")); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/test/java/com/thealgorithms/strings/RemoveStarsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.thealgorithms.strings; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class RemoveStarsTest { | ||
|
|
||
| @Test | ||
| void testExampleCase() { | ||
| assertEquals("lecoe", RemoveStars.removeStars("leet**cod*e")); | ||
| } | ||
|
|
||
| @Test | ||
| void testAllStars() { | ||
| assertEquals("", RemoveStars.removeStars("abc***")); | ||
| } | ||
|
|
||
| @Test | ||
| void testNoStars() { | ||
| assertEquals("hello", RemoveStars.removeStars("hello")); | ||
| } | ||
|
|
||
| @Test | ||
| void testSingleCharacter() { | ||
| assertEquals("", RemoveStars.removeStars("a*")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.