Repository: TheAlgorithms/Java
Pull request #5791 (2024-10-14 03:44:46)

feat: Add `AllConstruct` new algorithm with Junit tests
<!--
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/5791?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 `94.44444%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 64.26%. Comparing base [(`213fd5a`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/213fd5a4931ba1cb74fed76ff97043ae34ff174b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`9415cf1`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9415cf13412130e9b538802a50e8ffbfa8c3caed?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/5791?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...thealgorithms/dynamicprogramming/AllConstruct.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5791?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdynamicprogramming%2FAllConstruct.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9keW5hbWljcHJvZ3JhbW1pbmcvQWxsQ29uc3RydWN0LmphdmE=) | 94.44% | [1 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5791?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    #5791      +/-   ##
============================================
+ Coverage     64.21%   64.26%   +0.04%     
- Complexity     4216     4224       +8     
============================================
  Files           588      589       +1     
  Lines         16426    16444      +18     
  Branches       3170     3176       +6     
============================================
+ Hits          10548    10567      +19     
  Misses         5448     5448              
+ Partials        430      429       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5791?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).

siriak: Please check why build fails
Hardvan: I have fixed the issue with the build job.

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 7bc9cea3..9ebbd484 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -275,6 +275,7 @@
             * [TilingProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/TilingProblem.java)
           * dynamicprogramming
             * [Abbreviation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/Abbreviation.java)
+            * [AllConstruct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/AllConstruct.java)
             * [AssignmentUsingBitmask](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/AssignmentUsingBitmask.java)
             * [BoardPath](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java)
             * [BoundaryFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoundaryFill.java)
@@ -930,6 +931,7 @@
             * [TilingProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/TilingProblemTest.java)
           * dynamicprogramming
             * [AbbreviationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AbbreviationTest.java)
+            * [AllConstructTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AllConstructTest.java)
             * [AssignmentUsingBitmaskTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AssignmentUsingBitmaskTest.java)
             * [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
             * [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/AllConstruct.java b/src/main/java/com/thealgorithms/dynamicprogramming/AllConstruct.java
new file mode 100644
index 00000000..e7712b13
--- /dev/null
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/AllConstruct.java
@@ -0,0 +1,61 @@
+package com.thealgorithms.dynamicprogramming;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This class provides a solution to the "All Construct" problem.
+ *
+ * The problem is to determine all the ways a target string can be constructed
+ * from a given list of substrings. Each substring in the word bank can be used
+ * multiple times, and the order of substrings matters.
+ *
+ * @author Hardvan
+ */
+public final class AllConstruct {
+    private AllConstruct() {
+    }
+
+    /**
+     * Finds all possible ways to construct the target string using substrings
+     * from the given word bank.
+     * Time Complexity: O(n * m * k), where n = length of the target,
+     * m = number of words in wordBank, and k = average length of a word.
+     *
+     * Space Complexity: O(n * m) due to the size of the table storing combinations.
+     *
+     * @param target   The target string to construct.
+     * @param wordBank An iterable collection of substrings that can be used to construct the target.
+     * @return A list of lists, where each inner list represents one possible
+     *         way of constructing the target string using the given word bank.
+     */
+    public static List<List<String>> allConstruct(String target, Iterable<String> wordBank) {
+        List<List<List<String>>> table = new ArrayList<>(target.length() + 1);
+
+        for (int i = 0; i <= target.length(); i++) {
+            table.add(new ArrayList<>());
+        }
+
+        table.get(0).add(new ArrayList<>());
+
+        for (int i = 0; i <= target.length(); i++) {
+            if (!table.get(i).isEmpty()) {
+                for (String word : wordBank) {
+                    if (i + word.length() <= target.length() && target.substring(i, i + word.length()).equals(word)) {
+
+                        List<List<String>> newCombinations = new ArrayList<>();
+                        for (List<String> combination : table.get(i)) {
+                            List<String> newCombination = new ArrayList<>(combination);
+                            newCombination.add(word);
+                            newCombinations.add(newCombination);
+                        }
+
+                        table.get(i + word.length()).addAll(newCombinations);
+                    }
+                }
+            }
+        }
+
+        return table.get(target.length());
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/AllConstructTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/AllConstructTest.java
new file mode 100644
index 00000000..4979327f
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/AllConstructTest.java
@@ -0,0 +1,38 @@
+package com.thealgorithms.dynamicprogramming;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.Arrays;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+public class AllConstructTest {
+
+    @Test
+    public void testAllConstructBasic() {
+        List<List<String>> expected = Arrays.asList(Arrays.asList("he", "l", "l", "o"));
+        List<List<String>> result = AllConstruct.allConstruct("hello", Arrays.asList("he", "l", "o"));
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void testAllConstructMultipleWays() {
+        List<List<String>> expected = Arrays.asList(Arrays.asList("purp", "le"), Arrays.asList("p", "ur", "p", "le"));
+        List<List<String>> result = AllConstruct.allConstruct("purple", Arrays.asList("purp", "p", "ur", "le", "purpl"));
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void testAllConstructNoWays() {
+        List<List<String>> expected = Arrays.asList();
+        List<List<String>> result = AllConstruct.allConstruct("abcdef", Arrays.asList("gh", "ijk"));
+        assertEquals(expected, result);
+    }
+
+    @Test
+    public void testAllConstructEmptyTarget() {
+        List<List<String>> expected = Arrays.asList(Arrays.asList());
+        List<List<String>> result = AllConstruct.allConstruct("", Arrays.asList("a", "b", "c"));
+        assertEquals(expected, result);
+    }
+}