Repository: TheAlgorithms/Java
Pull request #5503 (2024-10-01 14:22:53)

feat: recursion subsets added
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5503?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `53.84615%` with `6 lines` in your changes missing coverage. Please review.
> Project coverage is 52.30%. Comparing base [(`7f60d57`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7f60d57504ab8456b72057735a0dc93595ad847f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`ee3cacb`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ee3cacbeb55c27a232963369d56293fff3e71362?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5503?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...main/java/com/thealgorithms/Recursion/Subsets.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5503?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2FRecursion%2FSubsets.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9SZWN1cnNpb24vU3Vic2V0cy5qYXZh) | 53.84% | [6 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5503?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5503      +/-   ##
============================================
+ Coverage     52.28%   52.30%   +0.02%     
- Complexity     3256     3259       +3     
============================================
  Files           525      526       +1     
  Lines         15185    15198      +13     
  Branches       2887     2887              
============================================
+ Hits           7940     7950      +10     
- Misses         6920     6925       +5     
+ Partials        325      323       -2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5503?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java
new file mode 100644
index 00000000..417bf130
--- /dev/null
+++ b/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java
@@ -0,0 +1,36 @@
+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 str) {
+        return doRecursion("", str);
+    }
+
+    private static List<String> doRecursion(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 = doRecursion(p + ch, up.substring(1));
+        // Not adding the character in the recursion
+        List<String> right = doRecursion(p, up.substring(1));
+
+        left.addAll(right);
+
+        return left;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java
new file mode 100644
index 00000000..d4bc7e48
--- /dev/null
+++ b/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java
@@ -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);
+    }
+}