<?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>interleaving Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/interleaving/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/interleaving/</link>
	<description></description>
	<lastBuildDate>Mon, 20 Apr 2020 07:18:54 +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>interleaving Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/interleaving/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1417. Reformat The String</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1417-reformat-the-string/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1417-reformat-the-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 20 Apr 2020 07:13:16 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[interleaving]]></category>
		<category><![CDATA[queue]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6643</guid>

					<description><![CDATA[<p>Given alphanumeric string&#160;s. (Alphanumeric string&#160;is a string consisting of lowercase English letters and digits). You have to find a permutation of&#160;the string where no letter&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1417-reformat-the-string/">花花酱 LeetCode 1417. Reformat The String</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>Given alphanumeric string&nbsp;<code>s</code>. (<strong>Alphanumeric string</strong>&nbsp;is a string consisting of lowercase English letters and digits).</p>



<p>You have to find a permutation of&nbsp;the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.</p>



<p>Return&nbsp;<em>the reformatted string</em>&nbsp;or return&nbsp;<strong>an empty string</strong>&nbsp;if it is impossible to reformat the string.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "a0b1c2"
<strong>Output:</strong> "0a1b2c"
<strong>Explanation:</strong> No two adjacent characters have the same type in "0a1b2c". "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> ""
<strong>Explanation:</strong> "leetcode" has only characters so we cannot separate them by digits.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1229857369"
<strong>Output:</strong> ""
<strong>Explanation:</strong> "1229857369" has only digits so we cannot separate them by characters.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "covid2019"
<strong>Output:</strong> "c2o0v1i9d"
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "ab123"
<strong>Output:</strong> "1a2b3"
</pre>



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



<ul><li><code>1 &lt;= s.length &lt;= 500</code></li><li><code>s</code>&nbsp;consists of only lowercase English letters and/or digits.</li></ul>



<h2><strong>Solution: Two streams</strong></h2>



<p>Create two stacks, one for alphas, another for numbers. If the larger stack has more than one element than the other one then no solution, return &#8220;&#8221;. Otherwise, interleave two stacks, start with the larger one.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string reformat(string s) {
    string q1;
    string q2;
    for (char c : s) {
      if (isalpha(c))
        q1 += c;
      else
        q2 += c;
    }    
    if (q1.length() &lt; q2.length())
      swap(q1, q2);
    if (q1.length() &gt; q2.length() + 1) 
      return &quot;&quot;;
    string ans;
    while (!q1.empty()) {
      ans += q1.back();
      q1.pop_back();
      if (q2.empty()) break;
      ans += q2.back();
      q2.pop_back();
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1417-reformat-the-string/">花花酱 LeetCode 1417. Reformat The String</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-1417-reformat-the-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 97. Interleaving String</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-97-interleaving-string/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-97-interleaving-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 06 Aug 2018 05:41:30 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[interleaving]]></category>
		<category><![CDATA[O(mn)]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3450</guid>

					<description><![CDATA[<p>Problem Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Example 2:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-97-interleaving-string/">花花酱 LeetCode 97. Interleaving String</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 <em>s1</em>, <em>s2</em>, <em>s3</em>, find whether <em>s3</em> is formed by the interleaving of <em>s1</em> and <em>s2</em>.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> s1 = "aabcc", s2 = "dbbca", <em>s3</em> = "aadbbcbcac"
<strong>Output:</strong> true
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> s1 = "aabcc", s2 = "dbbca", <em>s3</em> = "aadbbbaccc"
<strong>Output:</strong> false
</pre>
<h1><strong>Solution: DP</strong></h1>
<p>Subproblems : whether s3[0:i+j] can be formed by interleaving s1[0:i] and s2[0:j].</p>
<p>Time complexity: O(mn)</p>
<p>Space complexity: O(mn)</p><pre class="crayon-plain-tag">// Author: Huhaua
// Running time: 0 ms
class Solution {
public:
  bool isInterleave(string s1, string s2, string s3) {
    int l1 = s1.length();
    int l2 = s2.length();
    int l3 = s3.length();
    if (l1 + l2 != l3) return false;
    // dp[i][j]: whehter s3[0:i+j] is a interleva of s1[0:i] and s2[0:j]
    vector&lt;vector&lt;int&gt;&gt; dp(l1 + 1, vector&lt;int&gt;(l2 + 1));
    dp[0][0] = true;
    for (int i = 0; i &lt;= l1; ++i)
      for (int j = 0; j &lt;= l2; ++j) {        
        if (i &gt; 0) dp[i][j] |= dp[i - 1][j] &amp;&amp; s1[i - 1] == s3[i + j - 1];
        if (j &gt; 0) dp[i][j] |= dp[i][j - 1] &amp;&amp; s2[j - 1] == s3[i + j - 1];        
      }
    return dp[l1][l2];
  }
};</pre><p>Recursion + Memorization</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Solution {
public:
  bool isInterleave(string s1, string s2, string s3) {
    m_ = vector&lt;vector&lt;int&gt;&gt;(s1.length() + 1, vector&lt;int&gt;(s2.length() + 1, INT_MIN));
    return dp(s1, s1.length(), s2, s2.length(), s3, s3.length());
  }
private:
  // m_[i][j]: whehter s3[0:i+j] is a interleva of s1[0:i] and s2[0:j]
  vector&lt;vector&lt;int&gt;&gt; m_;
  
  bool dp(const string&amp; s1, int l1, const string&amp; s2, int l2, const string&amp; s3, int l3) {
    if (l1 + l2 != l3) return false;
    if (l1 == 0 &amp;&amp; l2 == 0) return true;
    if (m_[l1][l2] != INT_MIN) return m_[l1][l2];
    if (l1 &gt; 0 &amp; s3[l3 - 1] == s1[l1 - 1] &amp;&amp; dp(s1, l1 - 1, s2, l2, s3, l3 - 1)
      ||l2 &gt; 0 &amp; s3[l3 - 1] == s2[l2 - 1] &amp;&amp; dp(s1, l1, s2, l2 - 1, s3, l3 - 1))
      m_[l1][l2] = 1;
    else
      m_[l1][l2] = 0;
    return m_[l1][l2];
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-97-interleaving-string/">花花酱 LeetCode 97. Interleaving String</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/dynamic-programming/leetcode-97-interleaving-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
