139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
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
解法:
通过观察不难发现,如果s可以被wordDict中的字符串完整的拼出,那么wordDict中必然至少存在一个字符串,能完整匹配以s为开头的字符子串。例如,cats和cat均能完整匹配catsandog的开头部分。反之,如果wordDict中没有任何的一个字符串可以匹配以s为开头的字符子串,那就说明wordDict不能完整匹配出s字符串。(翻译成白话就是:你连s的开头部分都没有一个能匹配上的,后面的就更不用看了)
现在假定用wordDict中的cats去匹配s,那么现在s中还未被匹配上的还有andog,相当于现在假定s=andog后,再重复刚才的步骤去用wordDict进行匹配。如果最后都能匹配上,可见s最终会等于""。
在递归匹配的过程中,其实可以有个优化。在匹配的过程中,有可能会产生相同的s,如果在之前的匹配中知道这个s已经不能匹配上,那就无需重复进行尝试了。所以我们这里可以用一个Set去记录这些不能匹配成功的字符串。
代码:
public static boolean wordBreak(String s, List<String> wordDict) {
return wordBreak(s, wordDict, new HashSet<>()) ;
}
public static boolean wordBreak(String s, List<String> wordDict, Set<String> mismatchesCache) {
if (s.length() < 1) {
return true;
}
if(mismatchesCache.contains(s)) {
return false ;
}
for(String replace : wordDict) {
if(s.startsWith(replace) && wordBreak(s.substring(replace.length()), wordDict, mismatchesCache)) {
return true ;
}
}
mismatchesCache.add(s) ;
return false ;
}