首页 > 其他 > 详细

Word Break II

时间:2014-02-19 18:46:17      阅读:404      评论:0      收藏:0      [点我收藏+]

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

 

bubuko.com,布布扣
// DFS
public class Solution {
    public ArrayList<String> wordBreak(String s, Set<String> dict) {
        ArrayList<String> result = new ArrayList<String>();
        if(s == null || s.length() == 0)
            return result;
        
        int len = s.length();
        boolean[] dp = new boolean[len+1];
        dp[0] = true;
        for(int i = 0; i< len ; i++){
            if(!dp[i])
                continue;
            
            for(String e : dict){
                int end = i+ e.length();
                if(end > len)
                    continue;
                String sub = s.substring(i, end);
                if(dict.contains(sub))
                    dp[end]  = true;
            }
        }
        
        if(dp[len] == false) return result;
        StringBuilder  sb = new StringBuilder();
        dfs(0, sb, s, dict, result);
        return result;
    }
    
    private void dfs(int start, StringBuilder sb, String s, Set<String> dict, ArrayList<String> result){
        int len = s.length();
        if(start >= len){
            result.add(new String(sb));
            return;
        }
        
        for(int i = start+1; i<= len; i++){
            String sub = s.substring(start, i);
            if(dict.contains(sub)){
                int oldLen = sb.length();
                if(oldLen != 0){
                    sb.append(" ");
                }
                sb.append(sub);
                dfs(i, sb, s, dict, result);
                sb.delete(oldLen, sb.length());
            }
        }
    }
}
bubuko.com,布布扣

 

 

 

DP + Backtrack

bubuko.com,布布扣
    // DP + back track
    public ArrayList<String> wordBreak2(String s, Set<String> dict) {
        int n = s.length();
        ArrayList<ArrayList<Integer>> pres = new ArrayList<ArrayList<Integer>>(
                n);
        // initialize
        for (int i = 0; i < n; ++i)
            pres.add(new ArrayList<Integer>());
        // DP. pres[i] stores position j where should insert space
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j <= i; ++j) {
                String suffix = s.substring(j, i + 1);
                if ((j == 0 || pres.get(j - 1).size() > 0) && dict.contains(suffix))
                    pres.get(i).add(j);
            }
        }
        return getPath(s, n, pres);
    }

    public ArrayList<String> getPath(String s, int n, ArrayList<ArrayList<Integer>> pres) {
        ArrayList<String> res = new ArrayList<String>();
        for (int pre : pres.get(n - 1)) {
            if (pre == 0) {
                res.add(s.substring(0, n));
            } else {
                ArrayList<String> preres = getPath(s, pre, pres);
                String sub = s.substring(pre, n);
                for (String ss : preres)
                    res.add(ss + " " + sub);
            }
        }
        return res;
    }
bubuko.com,布布扣

Word Break II

原文:http://www.cnblogs.com/RazerLu/p/3555217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!