Word Break II Leetcode

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.

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

UPDATE (2017/1/4):
The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

 

 
这道题是返回所有的可能值,用dfs或者叫backtracking做。我发现每次新建一个list就不用remove了。。。但是必须有返回值,helper function不能是void。这难道就是dfs和backtracking的区别???不过至今还是觉得他俩好像一样的。。。好像没啥区别。。。
public class Solution {
    public List<String> wordBreak(String s, List<String> wordDict) {
        if (s == null || s.length() == 0) {
            return new ArrayList<>(0);
        }
        Map<String, List<String>> hm = new HashMap<>();
        return helper(s, wordDict, 0, hm);
    }
    public List<String> helper(String s, List<String> wordDict, int start, Map<String, List<String>> hm) {
        List<String> result = new ArrayList<>();
        if (hm.containsKey(s.substring(start, s.length()))) {
            return hm.get(s.substring(start, s.length()));
        }
        if (start == s.length()) {
            result.add("");
            return result;
        }
        for (int i = start; i < s.length(); i++) {
            if (wordDict.contains(s.substring(start, i + 1))) {
                List<String> tmp = new ArrayList<>();
                tmp = helper(s, wordDict, i + 1, hm);
                for (String t : tmp) {
                    result.add(s.substring(start, i + 1) + (t.equals("") ? "" : " ") + t);
                }
            }
        }
        hm.put(s.substring(start, s.length()), result);
        return result;
    }
}

这个方法还是稍微有点绕的,也不知道下次能不能写出来。。。还是及时回顾一下吧!

还有个方法是反向bfs,因为wordDict的size可能比较小,这样反向寻找可能比contains快一些。

public class Solution {
    public List<String> wordBreak(String s, List<String> wordDict) {
        if (s == null || s.length() == 0) {
            return new ArrayList<String>(0);
        }
        return helper(s, wordDict, new HashMap<String, List<String>>());
    }
    private List<String> helper(String s, List<String> wordDict, Map<String, List<String>> hm) {
        if (hm.containsKey(s)) {
            return hm.get(s);
        }
        List<String> result = new ArrayList<>();
        if (s.length() == 0) {
            result.add("");
            return result;
        }
        for (String word : wordDict) {
            if (s.startsWith(word)) {
                List<String> tmp = helper(s.substring(word.length()), wordDict, hm);
                for (String str : tmp) {
                    result.add(word + (str.equals("") ? "" : " ") + str);
                }
            }
        }
        hm.put(s, result);
        return result;
    }
}

需要注意的是,如果wordDict里面有"",就会造成stackoverflow。所以要处理一下。

        if (wordDict.contains("")) {
            wordDict.remove("");
        }

 

 
 
posted @ 2017-03-24 02:49  璨璨要好好学习  阅读(120)  评论(0编辑  收藏  举报