<?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>permuation Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/permuation/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/permuation/</link>
	<description></description>
	<lastBuildDate>Mon, 20 Aug 2018 03:29:40 +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>permuation Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/permuation/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 890. Find and Replace Pattern</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-890-find-and-replace-pattern/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-890-find-and-replace-pattern/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 20 Aug 2018 03:29:33 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[permuation]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3637</guid>

					<description><![CDATA[<p>Problem You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if there exists&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-890-find-and-replace-pattern/">花花酱 LeetCode 890. Find and Replace Pattern</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>Problem</h1>
<p>You have a list of <code>words</code> and a <code>pattern</code>, and you want to know which words in <code>words</code> matches the pattern.</p>
<p>A word matches the pattern if there exists a permutation of letters <code>p</code> so that after replacing every letter <code>x</code> in the pattern with <code>p(x)</code>, we get the desired word.</p>
<p>(<em>Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.</em>)</p>
<p>Return a list of the words in <code>words</code> that match the given pattern.</p>
<p>You may return the answer in any order.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>words = <span id="example-input-1-1">["abc","deq","mee","aqq","dkd","ccc"]</span>, pattern = <span id="example-input-1-2">"abb"</span>
<strong>Output: </strong><span id="example-output-1">["mee","aqq"]</span>
<strong>Explanation: </strong>"mee" matches the pattern because there is a permutation {a -&gt; m, b -&gt; e, ...}. 
"ccc" does not match the pattern because {a -&gt; c, b -&gt; c, ...} is not a permutation,
since a and b map to the same letter.</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>1 &lt;= words.length &lt;= 50</code></li>
<li><code>1 &lt;= pattern.length = words[i].length &lt;= 20</code></li>
</ul>
<p><ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-2404451723245401"
     data-ad-slot="7983117522"></ins></p>
<h1>Solution: Remember the last pos of each char.</h1>
<p>Time complexity: O(n*l)</p>
<p>Space complexity: O(128) -&gt; O(26)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Solution {
public:
  vector&lt;string&gt; findAndReplacePattern(vector&lt;string&gt;&amp; words, string pattern) {    
    vector&lt;string&gt; ans;
    for (const string&amp; w : words)
      if (match(w, pattern)) ans.push_back(w);
    return ans;
  }
private:
  bool match(const string&amp; w, const string&amp; p) {
    vector&lt;int&gt; last_pos_w(128, INT_MIN); // last pos of x in w
    vector&lt;int&gt; last_pos_p(128, INT_MIN); // last pos of x in p
    for (int i = 0; i &lt; w.size(); ++i)
      if (last_pos_w[w[i]] != last_pos_p[p[i]]) {
        return false;
      } else {
        last_pos_w[w[i]] = last_pos_p[p[i]] = i;
      }
    return true;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-890-find-and-replace-pattern/">花花酱 LeetCode 890. Find and Replace Pattern</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-890-find-and-replace-pattern/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 567. Permutation in String</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-567-permutation-in-string/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-567-permutation-in-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 29 Jun 2018 05:13:00 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[permuation]]></category>
		<category><![CDATA[sliding window]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[substring]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2957</guid>

					<description><![CDATA[<p>Problem 题目大意：给你s1, s2，问你s2的子串中是否存在s1的一个排列。 https://leetcode.com/problems/permutation-in-string/description/ Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string&#8217;s&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-567-permutation-in-string/">花花酱 LeetCode 567. Permutation in 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><iframe width="500" height="375" src="https://www.youtube.com/embed/wpq03MmEHIM?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>题目大意：给你s1, s2，问你s2的子串中是否存在s1的一个排列。</p>
<p><a href="https://leetcode.com/problems/permutation-in-string/description/">https://leetcode.com/problems/permutation-in-string/description/</a></p>
<p>Given two strings <b>s1</b> and <b>s2</b>, write a function to return true if <b>s2</b> contains the permutation of <b>s1</b>. In other words, one of the first string&#8217;s permutations is the <b>substring</b> of the second string.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b>s1 = "ab" s2 = "eidbaooo"
<b>Output:</b>True
<b>Explanation:</b> s2 contains one permutation of s1 ("ba").
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false"><b>Input:</b>s1= "ab" s2 = "eidboaoo"
<b>Output:</b> False
</pre>
<p><b>Note:</b></p>
<ol>
<li>The input strings only contain lower case letters.</li>
<li>The length of both given strings is in range [1, 10,000].</li>
</ol>
<p><img class="alignnone size-full wp-image-2962" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/567-ep200.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/567-ep200.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/567-ep200-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/06/567-ep200-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<h1><strong>Solution: Sliding Window</strong></h1>
<p>Time Complexity: O(l1 + l2 * 26) = O(l1 + l2)</p>
<p>Space Complexity: O(26 * 2) = O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 12 ms
class Solution {
public:
  bool checkInclusion(string s1, string s2) {
    int l1 = s1.length();
    int l2 = s2.length();    
    vector&lt;int&gt; c1(26);
    vector&lt;int&gt; c2(26);
    for (const char c : s1)
      ++c1[c - 'a'];
    for (int i = 0; i &lt; l2; ++i) {
      if (i &gt;= l1)
        --c2[s2[i - l1] - 'a'];
      ++c2[s2[i] - 'a'];      
      if (c1 == c2) return true;      
    }
    return false;
  }
};</pre><p></p>
<h1>Related Problems</h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/difficulty/hard/leetcode-480-sliding-window-median/">花花酱 LeetCode 480. Sliding Window Median</a></li>
<li><a href="http://zxi.mytechroad.com/blog/heap/leetcode-239-sliding-window-maximum/">花花酱 LeetCode 239. Sliding Window Maximum</a></li>
<li><a href="http://zxi.mytechroad.com/blog/string/leetcode-438-find-all-anagrams-in-a-string/">花花酱 LeetCode 438. Find All Anagrams in a String</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-567-permutation-in-string/">花花酱 LeetCode 567. Permutation in 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/hashtable/leetcode-567-permutation-in-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
