<?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>matching Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/matching/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/matching/</link>
	<description></description>
	<lastBuildDate>Sun, 21 Feb 2021 01:39:52 +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>matching Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/matching/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1764. Form Array by Concatenating Subarrays of Another Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1764-form-array-by-concatenating-subarrays-of-another-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1764-form-array-by-concatenating-subarrays-of-another-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 21 Feb 2021 00:32:23 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[kmp]]></category>
		<category><![CDATA[matching]]></category>
		<category><![CDATA[prefix]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8131</guid>

					<description><![CDATA[<p>You are given a 2D integer array&#160;groups&#160;of length&#160;n. You are also given an integer array&#160;nums. You are asked if you can choose&#160;n&#160;disjoint&#160;subarrays from the array&#160;nums&#160;such&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1764-form-array-by-concatenating-subarrays-of-another-array/">花花酱 LeetCode 1764. Form Array by Concatenating Subarrays of Another Array</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>You are given a 2D integer array&nbsp;<code>groups</code>&nbsp;of length&nbsp;<code>n</code>. You are also given an integer array&nbsp;<code>nums</code>.</p>



<p>You are asked if you can choose&nbsp;<code>n</code>&nbsp;<strong>disjoint&nbsp;</strong>subarrays from the array&nbsp;<code>nums</code>&nbsp;such that the&nbsp;<code>i<sup>th</sup></code>&nbsp;subarray is equal to&nbsp;<code>groups[i]</code>&nbsp;(<strong>0-indexed</strong>), and if&nbsp;<code>i &gt; 0</code>, the&nbsp;<code>(i-1)<sup>th</sup></code>&nbsp;subarray appears&nbsp;<strong>before</strong>&nbsp;the&nbsp;<code>i<sup>th</sup></code>&nbsp;subarray in&nbsp;<code>nums</code>&nbsp;(i.e. the subarrays must be in the same order as&nbsp;<code>groups</code>).</p>



<p>Return&nbsp;<code>true</code>&nbsp;<em>if you can do this task, and</em>&nbsp;<code>false</code>&nbsp;<em>otherwise</em>.</p>



<p>Note that the subarrays are&nbsp;<strong>disjoint</strong>&nbsp;if and only if there is no index&nbsp;<code>k</code>&nbsp;such that&nbsp;<code>nums[k]</code>&nbsp;belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
<strong>Output:</strong> true
<strong>Explanation:</strong> You can choose the 0<sup>th</sup> subarray as [1,-1,0,<strong>1,-1,-1</strong>,3,-2,0] and the 1<sup>st</sup> one as [1,-1,0,1,-1,-1,<strong>3,-2,0</strong>].
These subarrays are disjoint as they share no common nums[k] element.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
<strong>Output:</strong> false
<strong>Explanation: </strong>Note that choosing the subarrays [<strong>1,2,3,4</strong>,10,-2] and [1,2,3,4,<strong>10,-2</strong>] is incorrect because they are not in the same order as in groups.
[10,-2] must come before [1,2,3,4].
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
<strong>Output:</strong> false
<strong>Explanation: </strong>Note that choosing the subarrays [7,7,<strong>1,2,3</strong>,4,7,7] and [7,7,1,2,<strong>3,4</strong>,7,7] is invalid because they are not disjoint.
They share a common elements nums[4] (0-indexed).
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>groups.length == n</code></li><li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li><li><code>1 &lt;= groups[i].length, sum(groups[i].length) &lt;= 10<sup>3</sup></code></li><li><code>1 &lt;= nums.length &lt;= 10<sup>3</sup></code></li><li><code>-10<sup>7</sup>&nbsp;&lt;= groups[i][j], nums[k] &lt;= 10<sup>7</sup></code></li></ul>



<p></p>



<h2><strong>Solution: Brute Force</strong></h2>



<p>Time complexity: O(n^2?)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool canChoose(vector&lt;vector&lt;int&gt;&gt;&amp; groups, vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    int s = 0;
    for (const auto&amp; g : groups) {
      bool found = false;
      for (int i = s; i &lt;= n - g.size(); ++i)
        if (equal(begin(g), end(g), begin(nums) + i)) {
          s = i + g.size();
          found = true;
          break;
        }
      if (!found) return false;
    }
    return true;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1764-form-array-by-concatenating-subarrays-of-another-array/">花花酱 LeetCode 1764. Form Array by Concatenating Subarrays of Another Array</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/algorithms/array/leetcode-1764-form-array-by-concatenating-subarrays-of-another-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 10. Regular Expression Matching</title>
		<link>https://zxi.mytechroad.com/blog/programming-language/leetcode-10-regular-expression-matching/</link>
					<comments>https://zxi.mytechroad.com/blog/programming-language/leetcode-10-regular-expression-matching/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 17 Sep 2018 15:59:41 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[matching]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4007</guid>

					<description><![CDATA[<p>Problem Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/programming-language/leetcode-10-regular-expression-matching/">花花酱 LeetCode 10. Regular Expression Matching</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 input string (<code>s</code>) and a pattern (<code>p</code>), implement regular expression matching with support for <code>'.'</code> and <code>'*'</code>.</p>
<pre class="crayon:false ">'.' Matches any single character.
'*' Matches zero or more of the preceding element.
</pre>
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
<p><strong>Note:</strong></p>
<ul>
<li><code>s</code> could be empty and contains only lowercase letters <code>a-z</code>.</li>
<li><code>p</code> could be empty and contains only lowercase letters <code>a-z</code>, and characters like <code>.</code> or <code>*</code>.</li>
</ul>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>
s = "aa"
p = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> "a" does not match the entire string "aa".
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>
s = "aa"
p = "a*"
<strong>Output:</strong> true
<strong>Explanation:</strong> '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>
s = "ab"
p = ".*"
<strong>Output:</strong> true
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
</pre>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>
s = "aab"
p = "c*a*b"
<strong>Output:</strong> true
<strong>Explanation:</strong> c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
</pre>
<p><strong>Example 5:</strong></p>
<pre class="crayon:false"><strong>Input:</strong>
s = "mississippi"
p = "mis*is*p*."
<strong>Output:</strong> false
</pre>
<h1><strong>Solution 1: Recursion</strong></h1>
<p>Time complexity: O((|s| + |p|) * 2 ^ (|s| + |p|))</p>
<p>Space complexity: O(|s| + |p|)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 14 ms
class Solution {
public:
  bool isMatch(string s, string p) {
    return isMatch(s.c_str(), p.c_str());
  }
private:
  bool isMatch(const char* s, const char* p) {
    if (*p == '\0') return *s == '\0';
        
    // normal case, e.g. 'a.b','aaa', 'a'
    if (p[1] != '*' || p[1] == '\0') {
      // no char to match
      if (*s == '\0') return false;

      if (*s == *p || *p == '.')
        return isMatch(s + 1, p + 1);
      else
        return false;
    }
    else {
      int i = -1;
      while (i == -1 || s[i] == p[0] || p[0] == '.') {
          if (isMatch(s + i + 1, p + 2)) return true;
          if (s[++i] == '\0') break;
      }
      return false;
    }
    
    return false;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/programming-language/leetcode-10-regular-expression-matching/">花花酱 LeetCode 10. Regular Expression Matching</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/programming-language/leetcode-10-regular-expression-matching/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
