139. Word Break I & II

Word Break I

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".

public class Solution {
    public boolean wordBreak(String s, Set<String> dict) {
        if(s == null || dict == null) return false;
        boolean[] canSeg = new boolean[s.length()+1];
        // this is used when determine a whole word can be segmented or not
        canSeg[0] = true; 
        for(int i = 1; i <= s.length(); i++)
            for(int k = 0; k < i; k++)
                // if the whole word cannot be found in the dictionary, we can determine whether
                // all divided parts cannot be found in the dictionary.
                if(canSeg[k] && dict.contains(s.substring(k, i))) canSeg[i] = true;
        return canSeg[s.length()];
    }
}

上一个版本在面试讲解思路的时候,其实是非常麻烦的,下面这种方法使用的canSeg的长度是和s一样的,讲解思路的时候更容易,但是code稍微多一些。

 1 public boolean wordBreak(String s, Set<String> dict) {
 2       if(s == null || dict == null) return false;
 3       boolean[] canBreak = new boolean[s.length()];
 4       // initial case
 5       if (dict.contains(s.substring(0, 1))) {
 6           canBreak[0] = true;
 7       }
 8       for (int i = 1; i < s.length(); i++) {
 9           if (dict.contains(s.substring(0, i + 1))) {
10               canBreak[i] = true;
11           } else {
12               for (int j = 1; j <= i; j++) {
13                   String sub = s.substring(j, i + 1);
14                   if (canBreak[j - 1] && dict.contains(sub)) {
15                       canBreak[i] = true;
16                       break;
17                   }
18               }
19           }
20       }
21       return canBreak[canBreak.length - 1];
22   }

Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

分析:

 1 class Solution {
 2     public List<String> wordBreak(String s, List<String> wordDict) {
 3         return dfs(s, new HashSet<String>(wordDict), new HashMap<>());
 4     }       
 5 
 6     // DFS function returns an array including all substrings derived from s.
 7     List<String> dfs(String s, Set<String> wordDict, Map<String, List<String>>map) {
 8         if (map.containsKey(s)) {
 9             return map.get(s);
10         }
11 
12         List<String> res = new LinkedList<>();
13         
14         for (int i = 1; i <= s.length(); i++) {
15             String word = s.substring(0, i);
16             if (wordDict.contains(word)) {
17                 if (i == s.length()) {
18                     res.add(word);
19                 } else {
20                     List<String> sublist = dfs(s.substring(i), wordDict, map);
21                     for (String sub : sublist) {
22                         res.add(word + " " + sub);
23                     }
24                 }
25             }
26         }       
27         map.put(s, res);
28         return res;
29     }
30 }

 

posted @ 2015-01-26 05:49  北叶青藤  阅读(249)  评论(0)    收藏  举报