-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode139.java
More file actions
70 lines (64 loc) · 2.7 KB
/
LeetCode139.java
File metadata and controls
70 lines (64 loc) · 2.7 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Comparator;
public class LeetCode139 {
public static void main(String[] args) {
// 输入: s = "leetcode", wordDict = ["leet", "code"]
// 输出: true
System.out.println(new Solution139().wordBreak("leetcode", List.of("leet",
"code")));
// 输入: s = "applepenapple", wordDict = ["apple", "pen"]
// 输出: true
System.out.println(new Solution139().wordBreak("applepenapple",
List.of("apple", "pen")));
// 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
// 输出: false
System.out.println(new Solution139().wordBreak("catsandog", List.of("cats",
"dog", "sand", "and", "cat")));
// 输出: false
System.out.println(new Solution139().wordBreak(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
List.of("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa",
"aaaaaaaaa",
"aaaaaaaaaa")));
}
}
class Solution139 {
public boolean wordBreak(String s, List<String> wordDict) {
HashMap<Integer, HashSet<String>> dict = buildDict(wordDict);
boolean[] dp = new boolean[s.length()];
ArrayList<Integer> lens = new ArrayList<>(dict.keySet());
// NOTE: 贪心(先匹配长的)
lens.sort(Comparator.reverseOrder());
for (int i = 0; i < s.length(); i++) {
for (int len : lens) {
if (len == 1 + i) {
// 从头开始匹配
if (dict.get(len).contains(s.substring(0, i + 1))) {
dp[i] = true;
break;
}
} else if (len < 1 + i) {
if (dp[i - len] && dict.get(len).contains(s.substring(i - len + 1, i + 1))) {
dp[i] = true;
break;
}
}
}
}
return dp[s.length() - 1];
}
private HashMap<Integer, HashSet<String>> buildDict(List<String> wordDict) {
HashMap<Integer, HashSet<String>> dict = new HashMap<Integer, HashSet<String>>();
for (int i = 0; i < wordDict.size(); i++) {
String word = wordDict.get(i);
if (!dict.containsKey(word.length())) {
dict.put(word.length(), new HashSet<String>());
}
dict.get(word.length()).add(word);
}
return dict;
}
}