zoukankan      html  css  js  c++  java
  • 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"].

    // 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());
                }
            }
        }
    }

    DP + Backtrack

        // 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;
        }
  • 相关阅读:
    git 命令行下浏览器tig使用记录
    根据进程名字杀死进程
    centos7 在线安装postgresql9.5
    HttpClient使用代理访问
    nmap查看服务器端口使用情况
    java后台启动jar包
    不同语言,系统通过共享内存方式实现信息交互
    释放 MappedByteBuffer映射的内存
    Java使用ByteBuffer读取大文件
    Java共享内存
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3555217.html
Copyright © 2011-2022 走看看