【LeetCode-面試演算法經典-Java實現】【139-Word Break(單詞拆分)】

weixin_33860553發表於2017-07-26

【139-Word Break(單詞拆分)】


【LeetCode-面試演算法經典-Java實現】【全部題目資料夾索引】

原題

  Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
  For example, given
  s = "leetcode",
  dict = ["leet", "code"].
  Return true because "leetcode" can be segmented as "leet code".

題目大意

  給定一個字串s和單詞字典dict,確定s能否夠切割成一個或多個單詞空格分隔的序列。

解題思路

  一個字串S,它的長度為N。假設S能夠被“字典集合”(dict)中的單詞拼接而成,那麼所要滿足的條件為:
* F(0, N) = F(0, i) && F(i, j) && F(j, N);
* 這樣子,假設我們想知道某個子串是否可由Dict中的幾個單詞拼接而成就能夠用這種方式得到結果(滿足條件為True, 不滿足條件為False)存入到一個boolean陣列的相應位置上,這樣子,最後boolean 陣列的最後一位就是F(0, N)的值,為True表示這個字串S可由Dict中的單詞拼接,否則不行!

程式碼實現

演算法實現類

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution {

    public boolean wordBreak(String s, Set<String> wordDict) {
        // 引數校驗
        if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
            return false;
        }

        // 標記是否匹配,match[i]表表[0, i-1]都匹配
        int length = s.length();
        boolean[] match = new boolean[length + 1];
        match[0] = true;

        for (int i = 1; i < length + 1; i++) {
            for (int j = 0; j < i; j++) {
                if (match[j] && wordDict.contains(s.substring(j, i))) {
                    // f(0,n) = f(0,i) + f(i,j) + f(j,n)
                    match[i] = true;
                    break;
                }
            }
        }


        return match[length];
    }


    // 以下是還有一種解法。可是會超時
    public boolean wordBreak2(String s, Set<String> wordDict) {

        // 引數校驗
        if (s == null || s.length() < 1 || wordDict == null || wordDict.size() < 1) {
            return false;
        }

        Map<Character, Set<String>> wordMap = new HashMap<>(wordDict.size());
        // 將全部開始字元同樣的單詞放入一個Set中
        for (String word : wordDict) {
            Set<String> set = wordMap.get(word.charAt(0));
            if (set == null) {
                // 新建立一個set放入Map中
                set = new HashSet<>();
                wordMap.put(word.charAt(0), set);
            }
            // 單詞存入set中
            set.add(word);
        }

        return wordBreak(s, 0, wordMap);
    }

    /**
     * 搜尋字串能否夠被切割成單詞串
     *
     * @param s       字串
     * @param idx     處理的開始位置
     * @param wordMap 單詞字典,開始字元同樣的在同一個set集合中
     * @return 搜尋結果
     */
    public boolean wordBreak(String s, int idx, Map<Character, Set<String>> wordMap) {

        if (idx >= s.length()) {
            return true;
        }

        Set<String> words = wordMap.get(s.charAt(idx));

        if (words != null) {
            for (String word : words) {
                // idx之前的字元已經匹配,假設從ide之後起匹配word單詞
                if (s.startsWith(word, idx)) {
                    // 遞迴處理
                    boolean result = wordBreak(s, idx + word.length(), wordMap);
                    // 假設滿足條件,返回true
                    if (result) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}

評測結果

  點選圖片,滑鼠不釋放,拖動一段位置,釋放後在新的窗體中檢視完整圖片。

這裡寫圖片描寫敘述

特別說明

歡迎轉載。轉載請註明出處【http://blog.csdn.net/derrantcm/article/details/47774547

相關文章