<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>word break Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/word-break/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/word-break/</link>
	<description></description>
	<lastBuildDate>Wed, 29 Aug 2018 06:07:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>word break Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/word-break/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 Leetcode 140. Word Break II</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-140-word-break-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-140-word-break-ii/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Sep 2017 00:22:43 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[word break]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=52</guid>

					<description><![CDATA[<p>Problem 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&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-140-word-break-ii/">花花酱 Leetcode 140. Word Break II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/JqOIRBC0_9c?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Given a <b>non-empty</b> string <i>s</i> and a dictionary <i>wordDict</i> containing a list of <b>non-empty</b> words, add spaces in <i>s</i> to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.</p>
<p>Return all such possible sentences.</p>
<p>For example, given<br />
<i>s</i> = <code>"catsanddog"</code>,<br />
<i>dict</i> = <code>["cat", "cats", "and", "sand", "dog"]</code>.</p>
<p>A solution is <code>["cats and dog", "cat sand dog"]</code>.</p>
<p><b><span style="color: red;">UPDATE (2017/1/4):</span></b><br />
The <i>wordDict</i> 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.</p>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(2^n)</p>
<p>Space complexity: O(2^n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    vector&lt;string&gt; wordBreak(string s, vector&lt;string&gt;&amp; wordDict) {
        unordered_set&lt;string&gt; dict(wordDict.cbegin(), wordDict.cend());
        return wordBreak(s, dict);
    }
private:
    // &gt;&gt; append({"cats and", "cat sand"}, "dog");
    // {"cats and dog", "cat sand dog"}
    vector&lt;string&gt; append(const vector&lt;string&gt;&amp; prefixes, const string&amp; word) {
        vector&lt;string&gt; results;
        for(const auto&amp; prefix : prefixes)
            results.push_back(prefix + " " + word);
        return results;
    }
    
    const vector&lt;string&gt;&amp; wordBreak(string s, unordered_set&lt;string&gt;&amp; dict) {
        // Already in memory, return directly
        if(mem_.count(s)) return mem_[s];
        
        // Answer for s
        vector&lt;string&gt; ans;
        
        // s in dict, add it to the answer array
        if(dict.count(s)) 
            ans.push_back(s);
        
        for(int j=1;j&lt;s.length();++j) {
            // Check whether right part is a word
            const string&amp; right = s.substr(j);
            if (!dict.count(right)) continue;
            
            // Get the ans for left part
            const string&amp; left = s.substr(0, j);
            const vector&lt;string&gt; left_ans = 
                append(wordBreak(left, dict), right);
            
            // Notice, can not use mem_ here,
            // since we haven't got the ans for s yet.
            ans.insert(ans.end(), left_ans.begin(), left_ans.end());
        }
        
        // memorize and return
        mem_[s].swap(ans);
        return mem_[s];
    }
private:
    unordered_map&lt;string, vector&lt;string&gt;&gt; mem_;
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 56 ms
"""
class Solution:
  def wordBreak(self, s, wordDict):
    words = set(wordDict)
    mem = {}
    def wordBreak(s):
      if s in mem: return mem[s]
      ans = []
      if s in words: ans.append(s)
      for i in range(1, len(s)):
        right = s[i:]
        if right not in words: continue        
        ans += [w + " " + right for w in wordBreak(s[0:i])]
      mem[s] = ans
      return mem[s]
    return wordBreak(s)</pre><p>&nbsp;</p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/leetcode/leetcode-139-word-break/">[解题报告] Leetcode 139. Word Break 花花酱</a></li>
<li><a href="http://zxi.mytechroad.com/blog/searching/127-word-ladder/">[解题报告] LeetCode 127. Word Ladder</a></li>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-126-word-ladder-ii/">[解题报告] LeetCode 126. Word Ladder II</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-140-word-break-ii/">花花酱 Leetcode 140. Word Break II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/leetcode/leetcode-140-word-break-ii/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
