Skip to content

Commit 196cc60

Browse files
authored
Add AllConstruct algorithm (#5791)
1 parent 94daff0 commit 196cc60

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
* [TilingProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/TilingProblem.java)
276276
* dynamicprogramming
277277
* [Abbreviation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/Abbreviation.java)
278+
* [AllConstruct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/AllConstruct.java)
278279
* [AssignmentUsingBitmask](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/AssignmentUsingBitmask.java)
279280
* [BoardPath](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java)
280281
* [BoundaryFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoundaryFill.java)
@@ -930,6 +931,7 @@
930931
* [TilingProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/TilingProblemTest.java)
931932
* dynamicprogramming
932933
* [AbbreviationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AbbreviationTest.java)
934+
* [AllConstructTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AllConstructTest.java)
933935
* [AssignmentUsingBitmaskTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AssignmentUsingBitmaskTest.java)
934936
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
935937
* [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* This class provides a solution to the "All Construct" problem.
8+
*
9+
* The problem is to determine all the ways a target string can be constructed
10+
* from a given list of substrings. Each substring in the word bank can be used
11+
* multiple times, and the order of substrings matters.
12+
*
13+
* @author Hardvan
14+
*/
15+
public final class AllConstruct {
16+
private AllConstruct() {
17+
}
18+
19+
/**
20+
* Finds all possible ways to construct the target string using substrings
21+
* from the given word bank.
22+
* Time Complexity: O(n * m * k), where n = length of the target,
23+
* m = number of words in wordBank, and k = average length of a word.
24+
*
25+
* Space Complexity: O(n * m) due to the size of the table storing combinations.
26+
*
27+
* @param target The target string to construct.
28+
* @param wordBank An iterable collection of substrings that can be used to construct the target.
29+
* @return A list of lists, where each inner list represents one possible
30+
* way of constructing the target string using the given word bank.
31+
*/
32+
public static List<List<String>> allConstruct(String target, Iterable<String> wordBank) {
33+
List<List<List<String>>> table = new ArrayList<>(target.length() + 1);
34+
35+
for (int i = 0; i <= target.length(); i++) {
36+
table.add(new ArrayList<>());
37+
}
38+
39+
table.get(0).add(new ArrayList<>());
40+
41+
for (int i = 0; i <= target.length(); i++) {
42+
if (!table.get(i).isEmpty()) {
43+
for (String word : wordBank) {
44+
if (i + word.length() <= target.length() && target.substring(i, i + word.length()).equals(word)) {
45+
46+
List<List<String>> newCombinations = new ArrayList<>();
47+
for (List<String> combination : table.get(i)) {
48+
List<String> newCombination = new ArrayList<>(combination);
49+
newCombination.add(word);
50+
newCombinations.add(newCombination);
51+
}
52+
53+
table.get(i + word.length()).addAll(newCombinations);
54+
}
55+
}
56+
}
57+
}
58+
59+
return table.get(target.length());
60+
}
61+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class AllConstructTest {
10+
11+
@Test
12+
public void testAllConstructBasic() {
13+
List<List<String>> expected = Arrays.asList(Arrays.asList("he", "l", "l", "o"));
14+
List<List<String>> result = AllConstruct.allConstruct("hello", Arrays.asList("he", "l", "o"));
15+
assertEquals(expected, result);
16+
}
17+
18+
@Test
19+
public void testAllConstructMultipleWays() {
20+
List<List<String>> expected = Arrays.asList(Arrays.asList("purp", "le"), Arrays.asList("p", "ur", "p", "le"));
21+
List<List<String>> result = AllConstruct.allConstruct("purple", Arrays.asList("purp", "p", "ur", "le", "purpl"));
22+
assertEquals(expected, result);
23+
}
24+
25+
@Test
26+
public void testAllConstructNoWays() {
27+
List<List<String>> expected = Arrays.asList();
28+
List<List<String>> result = AllConstruct.allConstruct("abcdef", Arrays.asList("gh", "ijk"));
29+
assertEquals(expected, result);
30+
}
31+
32+
@Test
33+
public void testAllConstructEmptyTarget() {
34+
List<List<String>> expected = Arrays.asList(Arrays.asList());
35+
List<List<String>> result = AllConstruct.allConstruct("", Arrays.asList("a", "b", "c"));
36+
assertEquals(expected, result);
37+
}
38+
}

0 commit comments

Comments
 (0)