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

    Analysis:

    Use DP. d[i] = sum of (d[j]*s[j...i]) for j=i-1...0

    From URL: https://discuss.leetcode.com/topic/57248/3ms-99-91-java-bracktracking

    public class Solution {
        /* 3ms 99.91%
            Backtracking. Use a boolean array to prune branches.
            invalid[i]: s[i:end] can be breakable
            
            Also record max length of all words to prune branches.
        */
        int maxLen = 0;  // max length of all words
        public List<String> wordBreak(String s, Set<String> wordDict) {
            List<String> res = new ArrayList<>();
            for (String word : wordDict) if (word.length() > maxLen) maxLen = word.length();
            wordBreak(s, wordDict, new StringBuilder(), 0, new boolean[s.length()], res);
            return res;
        }
        private boolean wordBreak(String s, Set<String> wordDict, StringBuilder sb, int start, boolean[] invalid, List<String> res) {
            if (start == s.length()) { // reach the end
                res.add(sb.toString().trim());
                return true;
            }
            boolean breakable = false;
            int sbLen = sb.length();  // record current size
            int rightBound = Math.min(s.length(), start + maxLen);
            for (int end = start + 1; end <= rightBound; end++) {   // exclusive
                if (end != s.length() && invalid[end]) continue;    // check if s[right:end] is breakable
                String word = s.substring(start, end);
                if (wordDict.contains(word)) {
                    sb.append(" "); sb.append(word);
                    breakable |= wordBreak(s, wordDict, sb, end, invalid, res);
                    sb.setLength(sbLen);
                }
            }
            invalid[start] = !breakable;
            return breakable;
        }
    }
  • 相关阅读:
    C# 利用VS自带的WSDL工具生成WebService服务类
    七大管理工具
    在线教育系统
    Redis学习一
    酒店管理系统
    大数据学习一
    nginx负载均衡/反向代理学习一
    微服务学习十
    分布式学习一
    Abstract和Virtual和interface , 派生类中重写 override / new关键字
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4158879.html
Copyright © 2011-2022 走看看