<?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>pattern Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pattern/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pattern/</link>
	<description></description>
	<lastBuildDate>Sat, 08 Dec 2018 21:24:53 +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>pattern Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pattern/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 936. Stamping The Sequence</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-936-stamping-the-sequence/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-936-stamping-the-sequence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 08 Dec 2018 18:23:00 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[matching]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4406</guid>

					<description><![CDATA[<p>Problem You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-936-stamping-the-sequence/">花花酱 LeetCode 936. Stamping The Sequence</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/qTDYfhuqCVg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>You want to form a <code>target</code> string of <strong>lowercase letters</strong>.</p>
<p>At the beginning, your sequence is <code>target.length</code> <code>'?'</code> marks.  You also have a <code>stamp</code> of lowercase letters.</p>
<p>On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.  You can make up to <code>10 * target.length</code> turns.</p>
<p>For example, if the initial sequence is <span style="font-family: monospace;">&#8220;?????&#8221;</span>, and your stamp is <code>"abc"</code>,  then you may make <span style="font-family: monospace;">&#8220;abc??&#8221;, &#8220;?abc?&#8221;, &#8220;??abc&#8221; </span>in the first turn.  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)</p>
<p>If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn.  If the sequence is not possible to stamp, return an empty array.</p>
<p>For example, if the sequence is <span style="font-family: monospace;">&#8220;ababc&#8221;</span>, and the stamp is <code>"abc"</code>, then we could return the answer <code>[0, 2]</code>, corresponding to the moves <span style="font-family: monospace;">&#8220;?????&#8221; -&gt; &#8220;abc??&#8221; -&gt; &#8220;ababc&#8221;</span>.</p>
<p>Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within <code>10 * target.length</code> moves.  Any answers specifying more than this number of moves will not be accepted.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>stamp = <span id="example-input-1-1">"abc"</span>, target = <span id="example-input-1-2">"ababc"</span>
<strong>Output: </strong><span id="example-output-1">[0,2]</span>
([1,0,2] would also be accepted as an answer, as well as some other answers.)
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input: </strong>stamp = <span id="example-input-2-1">"</span><span id="example-input-2-2">abca</span>", target = <span id="example-input-2-2">"</span>aabcaca"
<strong>Output: </strong><span id="example-output-2">[3,0,1]</span></pre>
<div>
<p><strong>Note:</strong></p>
</div>
</div>
<ol>
<li><code>1 &lt;= stamp.length &lt;= target.length &lt;= 1000</code></li>
<li><code>stamp</code> and <code>target</code> only contain lowercase letters.</li>
</ol>
<h1><strong>Solution: Greedy + Reverse Simulation</strong></h1>
<p>Reverse the stamping process. Each time find a full or partial match. Replace the matched char to &#8216;?&#8217;.</p>
<p>Don&#8217;t forget the reverse the answer as well.</p>
<p>T = &#8220;ababc&#8221;, S = &#8220;abc&#8221;</p>
<p>T = &#8220;ab???&#8221;, index = 2</p>
<p>T = &#8220;?????&#8221;, index = 0</p>
<p>ans = [0, 2]</p>
<p>Time complexity: O((T &#8211; S)*S)</p>
<p>Space complexity: O(T)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, running time: 12 ms
class Solution {
public:
  vector&lt;int&gt; movesToStamp(string stamp, string target) {
    vector&lt;int&gt; ans;
    vector&lt;int&gt; seen(target.length());
    int total = 0;
    while (total &lt; target.length()) {      
      bool found = false;
      for (int i = 0; i &lt;= target.length() - stamp.length(); ++i) {
        if (seen[i]) continue;
        int l = unStamp(stamp, target, i);
        if (l == 0) continue;
        seen[i] = 1;
        total += l;
        ans.push_back(i);
        found = true;
      }
      if (!found) return {};
    }
    reverse(begin(ans), end(ans));
    return ans;
  }
private:
  int unStamp(const string&amp; stamp, string&amp; target, int s) {    
    int l = stamp.size();
    for (int i = 0; i &lt; stamp.length(); ++i) {
      if (target[s + i] == '?')
        --l;
      else if (target[s + i] != stamp[i])
        return 0;
    }
    
    if (l != 0)
      std::fill(begin(target) + s, begin(target) + s + stamp.length(), '?');
    return l;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-936-stamping-the-sequence/">花花酱 LeetCode 936. Stamping The Sequence</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/greedy/leetcode-936-stamping-the-sequence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 927. Three Equal Parts</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-927-three-equal-parts/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-927-three-equal-parts/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 21 Oct 2018 05:46:41 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[split]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4211</guid>

					<description><![CDATA[<p>Problem Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value. If it&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-927-three-equal-parts/">花花酱 LeetCode 927. Three Equal Parts</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given an array <code>A</code> of <code>0</code>s and <code>1</code>s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value.</p>
<p>If it is possible, return <strong>any</strong> <code>[i, j]</code> with <code>i+1 &lt; j</code>, such that:</p>
<ul>
<li><code>A[0], A[1], ..., A[i]</code> is the first part;</li>
<li><code>A[i+1], A[i+2], ..., A[j-1]</code> is the second part, and</li>
<li><code>A[j], A[j+1], ..., A[A.length - 1]</code> is the third part.</li>
<li>All three parts have equal binary value.</li>
</ul>
<p>If it is not possible, return <code>[-1, -1]</code>.</p>
<p>Note that the entire part is used when considering what binary value it represents.  For example, <code>[1,1,0]</code> represents <code>6</code> in decimal, not <code>3</code>.  Also, leading zeros are allowed, so <code>[0,1,1]</code> and <code>[1,1]</code> represent the same value.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">[1,0,1,0,1]</span>
<strong>Output: </strong><span id="example-output-1">[0,3]</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">[1,1,0,1,1]</span>
<strong>Output: </strong><span id="example-output-2">[-1,-1]</span></pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>3 &lt;= A.length &lt;= 30000</code></li>
<li><code>A[i] == 0</code> or <code>A[i] == 1</code></li>
</ol>
<h1><strong>Solution:</strong></h1>
<p>each part should have the same number of 1 s.</p>
<p>Find the suffix (without leading os) of the last part which should have 1/3 of the total ones.</p>
<p>Time complexity: O(n^2) in theory but close to O(n) in practice</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 44 ms
class Solution {
public:
  vector&lt;int&gt; threeEqualParts(vector&lt;int&gt;&amp; A) {    
    string s(begin(A), end(A));
    int ones = accumulate(begin(A), end(A), 0);
    if (ones % 3 != 0) return {-1, -1};
    if (ones == 0) return {0, A.size() - 1};
    ones /= 3;
    int right = A.size() - 1;
    while (ones) if (A[right--]) --ones;
    string suffix(begin(s) + right + 1, end(s));
    size_t l = suffix.length();
    size_t left = s.find(suffix);
    if (left == std::string::npos) return {-1, -1};
    size_t mid = s.find(suffix, left + l);
    if (mid == std::string::npos 
       || mid + 2 * l &gt; s.length()) return {-1, -1};
    return {left + l - 1, mid + l};
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-927-three-equal-parts/">花花酱 LeetCode 927. Three Equal Parts</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/string/leetcode-927-three-equal-parts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
