-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ139_WordBreak.java
More file actions
41 lines (36 loc) · 1.18 KB
/
Q139_WordBreak.java
File metadata and controls
41 lines (36 loc) · 1.18 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
package com.so.leetcode;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 139. 单词拆分
*
* @author FlyHippo
* @version 1.0
* @created 2025-05-17 16:07
* @tag
* @link <a href=""></a>
**/
public class Q139_WordBreak {
public boolean wordBreak(String s, List<String> wordDict) {
// 用 Set 提高查找效率
Set<String> wordSet = new HashSet<>(wordDict);
int n = s.length();
// dp[i] 表示:s 的前 i 个字符(即 s[0..i-1])是否可以由字典中的单词拼接出来
boolean[] dp = new boolean[n + 1];
dp[0] = true; // 空字符串可被拆分
// n是单词长度,
// 我们一个字符一个字符地考察 s 的前缀(即 s[0..i-1]),看看「前 i 个字符是否能拆成若干个字典单词」
for (int i = 1; i <= n; i++) {
//
for (int j = 0; j < i; j++) {
// dp[j] == true: 前 j 个已经包含; j 到 i 也包含
if (dp[j] && wordSet.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
}