-
Notifications
You must be signed in to change notification settings - Fork 87
/
WordBreak.java
35 lines (30 loc) · 1004 Bytes
/
WordBreak.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
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class WordBreak {
public static boolean wordBreak(String s, List<String> wordDict) {
Set<String> wordSet = new HashSet<>(wordDict);
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && wordSet.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
public static void main(String[] args) {
String s = "leetcode";
List<String> wordDict = List.of("leet", "code");
boolean result = wordBreak(s, wordDict);
if (result) {
System.out.println("The string can be segmented into valid words.");
} else {
System.out.println("The string cannot be segmented into valid words.");
}
}
}