leetcode *139. 單詞拆分

【題目】*139. 單詞拆分

給定一個非空字符串 s 和一個包含非空單詞列表的字典 wordDict,判定 s 是否可以被空格拆分爲一個或多個在字典中出現的單詞。

說明:
拆分時可以重複使用字典中的單詞。
你可以假設字典中沒有重複的單詞。

示例 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】動態規劃

假如需要判斷"onetwothreefour"這一個字符串能不能滿足條件,自然的想法就是:
如果"onetwothree"這一段可以拆分,"four"也可以,那"onetwothreefour"就可以
或者如果"onetwothre"這一段可以拆分,"efour"也可以,那"onetwothreefour"就可以

dp["onetwothreefour"] = dp["onetwothree"] && 判斷一下"four"
dp["onetwothreefour"] = dp["onetwothre"] && 判斷一下"efour"

定義dp[i]的含義: 字符串 s 前 i 個字符組成的字符串 s[0…i−1] 是否能被空格拆分成若干個字典中出現的單詞。
初始化/邊界條件:dp[0]=true 表示空串且合法。
狀態轉移方程
從前往後計算考慮轉移方程,需要枚舉 s[0…i-1] 中的分割點 j ,看 s[0…j-1] 組成的字符串s1(默認 j = 0 時 s1爲空串)和 s[j…i−1] 組成的字符串 s2是否都合法,如果兩個字符串均合法,那麼按照定義 s1和 s2拼接成的字符串也同樣合法。由於計算到 dp[i] 時我們已經計算出了 dp[0…i−1] 的值,因此字符串 s1是否合法可以直接由 dp[j] 得知,剩下的只需要看 s2是否合法即可,因此可以得出如下轉移方程:
dp[i] = dp[j] && check(s[j…i−1]),其中 check(s[j…i−1]) 表示子串 s[j…i−1] 是否出現在字典中。

對於檢查一個字符串是否出現在給定的字符串列表裏一般可以考慮哈希表來快速判斷

public class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> wordDictSet = new HashSet(wordDict);
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && wordDictSet.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }
}

可以做一些簡單的剪枝,枚舉分割點的時候倒着枚舉(從i-1的位置往0枚舉),如果分割點 j 到 i 的長度已經大於字典列表裏最長的單詞的長度,那麼就結束枚舉。

public class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> wordDictSet = new HashSet(wordDict);
        int max = 0; //最長的單詞長度
        for(String word: wordDictSet){
            max = Math.max(word.length(), max);
        }
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = i - 1; j >= 0; j--) {
                if(i - j > max){ //子串s2的長度已經大於最長的單詞長度
                    break;
                }
                if (dp[j] && wordDictSet.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }
}

【解題思路2】DFS(未研究)

BFS

public class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        return wordBreakHelper(s, wordDict, new ArrayList<>(), 0);
    }

    //start表示的是從字符串s的哪個位置開始
    public boolean wordBreakHelper(String s, List<String> wordDict, List<Integer> excludeList, int start) {
        if (start == s.length())
            return true;
        for (int end = start + 1; end <= s.length(); end++) {
            if (excludeList.contains(end))
                continue;
            if (wordDict.contains(s.substring(start, end))) {
                if (wordBreakHelper(s, wordDict, excludeList, end))
                    return true;
                excludeList.add(end);
            }
        }
        return false;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章