LeetCode-Word Break II

来源:互联网 发布:删除文件恢复软件 编辑:程序博客网 时间:2024/05/01 01:04

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 boolean wordBreak(String s, Set<String> wordDict) {        if (s == null || wordDict == null) return false;        return dfs(s, wordDict, 0);    }        private boolean dfs(String str, Set<String> wordDict, int s) {    if (s > str.length()) return false;    else if (s == str.length()) return true;    for (int i = s+1; i <= str.length(); i++) {    String sub = str.substring(s, i);    if (wordDict.contains(sub) && dfs(str, wordDict, i)) {    return true;    }    }    return false;    }

标准的回溯方法,但是超时。可以分析一下为什么超时,假设s="aaab",wordDict={"a", "aa", "aaa"};

看一看递归树:

               a                                                                   aa                    aaa                 aaab

     a           aa  aab                                              a   ab                        b

 a   ab        b                                                    b

b

可以看出重叠子问题,因为在没有找到匹配的时候,会全部遍历这课树,所以可以改用动态规划。

boolean[] can = new boolean[s.length()+1];        can[0] = true;// 空字符串        //f(0, i) = f(0, j) & f(j, i)  k = 0...n-1; i = 0...n        for (int i = 1; i <= s.length(); i++) {            for (int j = 0; j < i; j++) {                 if (can[j] && wordDict.contains(s.substring(j, i))) {                    can[i] = true;                    break;                }            }        }        return can[s.length()];

boolean数组can[i]是指,s.substring(0,i)是可以分割的,所示数组最后一个元素即为所求。

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

另外一个版本,can[i]只值以i为起点的字符串是否在wordDict中

接下来搞word breakII

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

因为要列出所有的可能,所以肯定要用dfs,一个很标准额DFS模板式解答:

public List<String> wordBreak(String s, Set<String> wordDict) {        List<String> list = new ArrayList<String>();    if (s == null || wordDict == null) return list;    dfs(list, s, wordDict, new StringBuilder(), 0);    return list;    }    private void dfs(List<String> list, String s, Set<String> dict, StringBuilder sb, int index) {    if (index == s.length()) {    list.add(new String(sb.toString()));    return;    }    for (int i = index+1; i <= s.length(); i++) {    StringBuilder temp = new StringBuilder(sb);    String sub = s.substring(index, i);    if (dict.contains(sub)) {    if (index != 0)    temp.append(" ");    temp.append(sub);    dfs(list, s, dict, temp, i);    }    }    }


 很显然超时了,怎么办?在DFS中只能剪枝了,我们可以根据word break得出,我们可以先在O(n2)的空间复杂度下跑一下wordbreak,根据其can[]数据进行剪枝

    public List<String> wordBreak(String s, Set<String> wordDict) {        List<String> list = new ArrayList<String>();    if (s == null || wordDict == null) return list;    dfs(list, s, wordDict, new StringBuilder(), check(s, wordDict), 0);    return list;    }    private void dfs(List<String> list, String s, Set<String> dict, StringBuilder sb, boolean[] can, int index) {    if (index == s.length()) {    list.add(new String(sb.toString()));    return;    }    for (int i = index+1; i <= s.length(); i++) {    String sub = s.substring(index, i);    StringBuilder temp = new StringBuilder(sb);    if (dict.contains(sub) && can[i]) {    if (index != 0)    temp.append(" ");    temp.append(sub);    dfs(list, s, dict, temp, can, i);    }    }    }        private boolean[] check(String s, Set<String> wordDict) {        boolean[] can = new boolean[s.length()+1];        can[s.length()] = true;        for (int i = s.length()-1; i >= 0; i--) {        for (int j = s.length(); j > i; j--) {        if (wordDict.contains(s.substring(i, j)) && can[j]) {        can[i] = true;        break;        }        }        }        return can;    }

在DFS的时候,不仅要保证subsring(i, j)在wordDic中,此时还要保证substring(j, length),此时效率要高很多,不错!








0 0