-
Notifications
You must be signed in to change notification settings - Fork 20.5k
feat: recursion subsets added #5503
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
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
37bb532
LinkedList added
Tuhinm2002 17d0ca3
LinkedList added
Tuhinm2002 ee3cacb
feat: recursion subsets added
Tuhinm2002 9e5057b
Update and rename SubsetsTest.java to GenerateSubsetsTest.java
Tuhinm2002 e49a414
Update and rename Subsets.java to GenerateSubsets.java
Tuhinm2002 89687c8
Update GenerateSubsets.java
Tuhinm2002 eceb162
Update GenerateSubsetsTest.java
Tuhinm2002 6d840a2
Update GenerateSubsets.java
Tuhinm2002 5043123
Update GenerateSubsets.java
Tuhinm2002 61a81c9
Update GenerateSubsets.java
Tuhinm2002 d3d154e
Update GenerateSubsetsTest.java
Tuhinm2002 25874a9
Update GenerateSubsets.java
Tuhinm2002 ba81599
Update GenerateSubsetsTest.java
Tuhinm2002 c14d684
Merge branch 'master' into master
Tuhinm2002 0d202a6
Merge branch 'master' into master
siriak 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/Recursion/GenerateSubsets.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.Recursion; | ||
|
||
// program to find power set of a string | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class GenerateSubsets { | ||
|
||
private GenerateSubsets() { | ||
throw new UnsupportedOperationException("Utility class"); | ||
} | ||
|
||
public static List<String> subsetRecursion(String p, String up) { | ||
if (up.isEmpty()) { | ||
List<String> list = new ArrayList<>(); | ||
list.add(p); | ||
return list; | ||
} | ||
|
||
// Taking the character | ||
char ch = up.charAt(0); | ||
// Adding the character in the recursion | ||
List<String> left = subsetRecursion(p + ch, up.substring(1)); | ||
// Not adding the character in the recursion | ||
List<String> right = subsetRecursion(p, up.substring(1)); | ||
|
||
left.addAll(right); | ||
|
||
return left; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.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,36 @@ | ||
package com.thealgorithms.Recursion; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public final class GenerateSubsetsTest { | ||
|
||
@Test | ||
void subsetRecursionTestOne() { | ||
String str = "abc"; | ||
String[] expected = new String[] {"abc", "ab", "ac", "a", "bc", "b", "c", ""}; | ||
|
||
List<String> ans = GenerateSubsets.subsetRecursion("", str); | ||
assertArrayEquals(ans.toArray(), expected); | ||
} | ||
|
||
@Test | ||
void subsetRecursionTestTwo() { | ||
String str = "cbf"; | ||
String[] expected = new String[] {"cbf", "cb", "cf", "c", "bf", "b", "f", ""}; | ||
|
||
List<String> ans = GenerateSubsets.subsetRecursion("", str); | ||
assertArrayEquals(ans.toArray(), expected); | ||
} | ||
|
||
@Test | ||
void subsetRecursionTestThree() { | ||
String str = "aba"; | ||
String[] expected = new String[] {"aba", "ab", "aa", "a", "ba", "b", "a", ""}; | ||
|
||
List<String> ans = GenerateSubsets.subsetRecursion("", str); | ||
assertArrayEquals(ans.toArray(), expected); | ||
} | ||
} |
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.