-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanConstruct.java
47 lines (35 loc) · 1.43 KB
/
CanConstruct.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Problem Statement:
------------------
Write a function `canConstruct(target, wordBank)` that accepts a target string and an array of strings.
The function should return a boolean indicating whether or not the `target` can be constructed by concatenating elements of the `wordBank` array.
You may reuse elements of `worldBank` as many times as needed.
Examples:
---------
canConstruct('abcdef', ['ab', 'abc', 'cd', 'def', 'abcd']) -> True
canConstruct('skateboard', ['bo', 'rd', 'ate', 't', 'ska', 'sk', 'boar']) -> False
canConstruct('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef', ['e', 'ee', 'eee', 'eeee', 'eeeee', 'eeeeee']) -> False
*/
public class CanConstruct {
public static boolean canConstruct(String target, String wordBank[]) {
// Base case:
if (target == "") {
return true;
}
for (int i = 0; i < wordBank.length; i++) {
// If the current word is present as prefix
if (target.indexOf(wordBank[i]) == 0) {
String remainingString = target.substring(wordBank[i].length());
if (canConstruct(remainingString, wordBank) == true) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
String target = "abcdef";
String wordBank[] = { "ab", "abc", "cd", "def", "abcd" };
System.out.println(canConstruct(target, wordBank));
}
}