forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Solution.java
45 lines (34 loc) · 1.01 KB
/
Solution.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
package eleven;
import java.util.HashSet;
import java.util.Set;
/**
* @author dmrfcoder
* @date 2019/4/10
*/
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
if (s == null) {
return false;
}
int sLength = s.length();
boolean[] array = new boolean[sLength + 1];
array[0] = true;
for (int index = 0; index <= sLength; index++) {
for (int index2 = 0; index2 < index; index2++) {
for (String dictItem : dict) {
if (s.substring(index2, index).equals(dictItem) && array[index2]) {
array[index] = true;
}
}
}
}
return array[sLength];
}
public static void main(String[] args) {
Set<String> dict = new HashSet<>();
dict.add("aaa");
dict.add("aaaa");
Solution solution = new Solution();
System.out.println(solution.wordBreak("aaaaaaa", dict));
}
}