Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,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)
Expand Down Expand Up @@ -320,6 +321,7 @@
* [BresenhamLine](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/BresenhamLine.java)
* [ConvexHull](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/ConvexHull.java)
* [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java)
* [MidpointEllipse](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/MidpointEllipse.java)
* [Point](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/Point.java)
* graph
* [StronglyConnectedComponentOptimized](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/graph/StronglyConnectedComponentOptimized.java)
Expand Down Expand Up @@ -920,6 +922,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)
Expand Down Expand Up @@ -967,6 +970,7 @@
* [BresenhamLineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/BresenhamLineTest.java)
* [ConvexHullTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java)
* [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
* [MidpointEllipseTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/MidpointEllipseTest.java)
* graph
* [StronglyConnectedComponentOptimizedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/graph/StronglyConnectedComponentOptimizedTest.java)
* greedyalgorithms
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}