139. 单词拆分
题目
描述:
给你一个字符串 s 和一个字符串列表 wordDict 作为字典。如果可以利用字典中出现的一个或多个单词拼接出 s 则返回 true。
注意: 不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。
示例 1:
- 输入:
s = "leetcode", wordDict = ["leet", "code"] - 输出:
true - 解释: 返回
true因为"leetcode"可以由"leet"和"code"拼接而成。
示例 2:
- 输入:
s = "applepenapple", wordDict = ["apple", "pen"] - 输出:
true - 解释: 返回
true因为"applepenapple"可以由"apple" "pen" "apple"拼接而成。注意,你可以重复使用字典中的单词。
示例 3:
- 输入:
s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] - 输出:
false
提示:
1 <= s.length <= 3001 <= wordDict.length <= 10001 <= wordDict[i].length <= 20s和wordDict[i]仅由小写英文字母组成wordDict中的所有字符串 互不相同
题解
判断前缀能否完全拆分
dp[i] 表示前 i 个字符,即半开区间 s[0,i),能否由词典单词拼成。最后一段若为 s[j,i),必须同时满足前 j 个字符已可拆分、最后这一段在词典中,才能使 dp[i] 为真。
因此从小到大枚举 i,再枚举 0 <= j < i。找到一个可行分割点就设置 dp[i] = true 并结束本轮,因为这里只判断是否存在,不需要统计全部拆分方案。
空前缀是递推的起点
设置 dp[0]=true,表示尚未使用字符时不需要单词。这样整个前缀本身就是词典单词时,j 为 0 的转移也能成功。例如 leetcode、词典为 leet 和 code,先由 dp[0] 得到 dp[4],再由 dp[4] 得到 dp[8]。
词典单词可以重复使用,所以不从集合中删除已用词。不能贪心地选择最长前缀,例如 cars 配词典 car、ca、rs,先选 car 会失败,而 ca + rs 可以成功。DP 会保留所有可行的前缀边界。
子串构造也要计入时间
HashSet 通过哈希定位词条,但对新建候选字符串,substring(j,i) 的复制和首次哈希计算都需要与长度成正比的时间,不能把整次查词当作与词长无关的常数。
设字符串长 n、词典总字符数为 K,当前 Java 实现的期望时间上界为 $O(K+n³)$,其中最多 $O(n²)$ 次候选检查,每次至多 $O(n)$。空间方面,状态数组占 $O(n)$,同一时刻存在的候选子串最多 $O(n)$ 个字符,词典集合按字符数计为 $O(K)$,总辅助空间为 $O(K+n)$。最终返回 dp[n],不要求构造具体拆分。
完整示例
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
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
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 s1 = "leetcode";
List<String> wordDict1 = Arrays.asList("leet", "code");
System.out.println("示例 1 输入: s = \"" + s1 + "\", wordDict = " + wordDict1);
System.out.println("示例 1 输出: " + wordBreak(s1, wordDict1));
System.out.println("-----------------------------");
String s2 = "applepenapple";
List<String> wordDict2 = Arrays.asList("apple", "pen");
System.out.println("示例 2 输入: s = \"" + s2 + "\", wordDict = " + wordDict2);
System.out.println("示例 2 输出: " + wordBreak(s2, wordDict2));
System.out.println("-----------------------------");
String s3 = "catsandog";
List<String> wordDict3 = Arrays.asList("cats", "dog", "sand", "and", "cat");
System.out.println("示例 3 输入: s = \"" + s3 + "\", wordDict = " + wordDict3);
System.out.println("示例 3 输出: " + wordBreak(s3, wordDict3));
}
}
运行结果(OpenJDK 21.0.12):
1
2
3
4
5
6
7
8
示例 1 输入: s = "leetcode", wordDict = [leet, code]
示例 1 输出: true
-----------------------------
示例 2 输入: s = "applepenapple", wordDict = [apple, pen]
示例 2 输出: true
-----------------------------
示例 3 输入: s = "catsandog", wordDict = [cats, dog, sand, and, cat]
示例 3 输出: false
dp[0] 表示空前缀可拆分。双层循环最多检查 O(n²) 对下标,截取和哈希子串可能各处理 O(n) 个字符,最坏时间为 O(n³),另有构造词典哈希表的成本。设词典有 D 个词,哈希表存储词引用,状态表与当前切片使用 O(n) 空间,合计辅助空间为 O(n + D)。
这题的经验是:字符串切分问题先定义“前缀是否可达”,会比直接回溯全切法更可控。
