[LeetCode] 139. Word Break
Given a string s
and a dictionary of strings wordDict
, return true
if s
can be segmented into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
Input: s = "leetcode", wordDict = ["leet","code"] Output: true Explanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple","pen"] Output: true Explanation: Return true because "applepenapple" can be segmented as "apple pen apple". Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] Output: false
Constraints:
1 <= s.length <= 300
1 <= wordDict.length <= 1000
1 <= wordDict[i].length <= 20
s
andwordDict[i]
consist of only lowercase English letters.- All the strings of
wordDict
are unique.
单词拆分。
给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出 s 。
注意:不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/word-break
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是 DP,DP[i] 的含义是以 index = i 位置上那个字母结尾的字符串是否能被 list 中的单词拼接。初始化 dp[0] = true。接下来用另外一个指针 j 去扫描 0 - i 范围内所有的的 substring。如果 dp[j] = true && substring(j, i)也在 wordDict 存在,则dp[i] = true。
跑一下第 1 个例子,
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"] Output: true Explanation: Return true because"leetcode"
can be segmented as"leet code"
.
DP 数组最后的输出值如下,当 i 指针遍历到 c(index = 4),j 指针还在 0 的时候,此时因为 dp[0] = true && s.substring(j, i) = s.substring(0, 4) = "leet" 也存在于 wordDict,所以可以将 dp[4] 标记为 true。
[true, false, false, false, true, false, false, false, true]
时间O(n^2)
空间O(n)
Java实现
1 class Solution { 2 public boolean wordBreak(String s, List<String> wordDict) { 3 boolean[] dp = new boolean[s.length() + 1]; 4 dp[0] = true; 5 for (int i = 1; i <= s.length(); i++) { 6 for (int j = 0; j < i; j++) { 7 if (dp[j] && wordDict.contains(s.substring(j, i))) { 8 dp[i] = true; 9 break; 10 } 11 } 12 } 13 return dp[s.length()]; 14 } 15 }
JavaScript实现
1 /** 2 * @param {string} s 3 * @param {string[]} wordDict 4 * @return {boolean} 5 */ 6 var wordBreak = function (s, wordDict) { 7 const dp = new Array(s.length + 1).fill(false); 8 dp[0] = true; 9 for (let i = 1; i <= s.length; i++) { 10 for (let j = 0; j < i; j++) { 11 const word = s.slice(j, i); 12 if (dp[j] == true && wordDict.includes(word)) { 13 dp[i] = true; 14 break; 15 } 16 } 17 } 18 return dp[s.length]; 19 };
我们注意到上一种方法可行,但是效率很低。因为 substring 函数会找到一些根本不存在于 wordDict 中的单词,比如我们可以找到 leet,但是我们再去找 leetc 是没有意义的,这样一个字母一个字母地加,效率很低。一个优化的方法是我们遍历位置的同时,第二层 for 循环遍历的是每个单词。比如一开始初始化 dp[0] = true,然后对于 wordDict 中的每个单词 word,我要找的下一个 substring 是 s.substring(i + word.length())。这样我就可以按照当前单词的长度,对这个环节的搜索加速。
时间O(n^2)
空间O(n)
Java实现
1 class Solution { 2 public boolean wordBreak(String s, List<String> wordDict) { 3 int n = s.length(); 4 boolean[] dp = new boolean[n + 1]; 5 dp[0] = true; 6 7 for (int lo = 0; lo < n; lo++) { 8 if (!dp[lo]) { 9 continue; 10 } 11 for (String word : wordDict) { 12 int hi = lo + word.length(); 13 // 因为substring是左闭右开的区间所以要判断hi <= n 14 if (hi <= n && s.substring(lo, hi).equals(word)) { 15 dp[hi] = true; 16 } 17 } 18 } 19 return dp[n]; 20 } 21 }