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

链接: http://leetcode.com/problems/word-break-ii/

题解:

又看到这种求所有解集的题目,自然就想到用DFS + Backtracking。在Word Ladder II里我们这样做,在这里我们依然这样做。感觉现在DFS往往伴随Backtracking,以后面试题估计这是一种常态。 这里需要注意的是对有个超长的case,我们要提前判断能否word break,所以还要用带一部分Word Break I里面的代码。  为图省事直接copy & paste了,其实应该还能重构一下,让代码不那么sloppy。回溯的部分依然是Word Break I的结构,使用了一个StringBuilder来回溯加入的单词以及空格。这回时间复杂度是真的O(2n)了。

Time Complexity - O(2n), Space Complexity - O(2n)

public class Solution {
    public List<String> wordBreak(String s, Set<String> wordDict) {
        List<String> res = new ArrayList<>();
        if(s == null || wordDict == null)
            return res;
        StringBuilder sb = new StringBuilder();
        if(canWordBreak(s, new HashSet<String>(wordDict)))      //find out if we can word break
            findAllWordBreak(res, sb, s, wordDict);
        return res;
    }
    
    private void findAllWordBreak(List<String> res, StringBuilder sb, String s, Set<String> wordDict) {
        if(s.length() == 0) {
            res.add(sb.toString().trim());
            return;
        }
        
        for(int i = 1; i <= s.length(); i++) {
            String frontPart = s.substring(0, i);
            String backPart = s.substring(i, s.length());
            if(wordDict.contains(frontPart)) {
                sb.append(frontPart);
                sb.append(" ");
                findAllWordBreak(res, sb, backPart, wordDict);
                sb.setLength(sb.length() - 1 - frontPart.length());
            }
        }
    }
    
     private boolean canWordBreak(String s, Set<String> wordDict) {     //Word Break I
        if(s == null || wordDict == null)
            return false;
        if(s.length() == 0)
            return true;
        
        for(int i = 1; i <= s.length(); i++) {
            String frontPart = s.substring(0, i);
            String backPart = s.substring(i, s.length());
            if(wordDict.contains(frontPart)) {
                if(canWordBreak(backPart, wordDict))
                    return true;
                wordDict.remove(frontPart);
            }
        }
        
        return false;
    }
}

 

Reference:

https://leetcode.com/discuss/27464/my-concise-answer

https://leetcode.com/discuss/23770/slightly-modified-dp-java-solution

https://leetcode.com/discuss/7439/this-accepted-java-version-program-there-any-better-solution

https://leetcode.com/discuss/133/is-there-better-solution-for-this-word-breakii

http://www.cnblogs.com/springfor/p/3877056.html

http://blog.csdn.net/linhuanmars/article/details/22452163

http://www.programcreek.com/2014/03/leetcode-word-break-ii-java/

http://www.acmerblog.com/word-break-ii-6128.html 

posted @ 2015-04-19 11:21  YRB  阅读(1667)  评论(0编辑  收藏  举报