<?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>anagram Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/anagram/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/anagram/</link>
	<description></description>
	<lastBuildDate>Sun, 27 Feb 2022 05:20:44 +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>anagram Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/anagram/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2186. Minimum Number of Steps to Make Two Strings Anagram II</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2186-minimum-number-of-steps-to-make-two-strings-anagram-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2186-minimum-number-of-steps-to-make-two-strings-anagram-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Feb 2022 05:18:20 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[anagram]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9509</guid>

					<description><![CDATA[<p>You are given two strings&#160;s&#160;and&#160;t. In one step, you can append&#160;any character&#160;to either&#160;s&#160;or&#160;t. Return&#160;the minimum number of steps to make&#160;s&#160;and&#160;t&#160;anagrams&#160;of each other. An&#160;anagram&#160;of a string&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2186-minimum-number-of-steps-to-make-two-strings-anagram-ii/">花花酱 LeetCode 2186. Minimum Number of Steps to Make Two Strings Anagram 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>You are given two strings&nbsp;<code>s</code>&nbsp;and&nbsp;<code>t</code>. In one step, you can append&nbsp;<strong>any character</strong>&nbsp;to either&nbsp;<code>s</code>&nbsp;or&nbsp;<code>t</code>.</p>



<p>Return&nbsp;<em>the minimum number of steps to make&nbsp;</em><code>s</code><em>&nbsp;and&nbsp;</em><code>t</code><em>&nbsp;<strong>anagrams</strong>&nbsp;of each other.</em></p>



<p>An&nbsp;<strong>anagram</strong>&nbsp;of a string is a string that contains the same characters with a different (or the same) ordering.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "<strong><u>lee</u></strong>tco<strong>de</strong>", t = "co<strong>a</strong>t<strong>s</strong>"
<strong>Output:</strong> 7
<strong>Explanation:</strong> 
- In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcode<strong><u>as</u></strong>".
- In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coats<strong>leede</strong>".
"leetcodeas" and "coatsleede" are now anagrams of each other.
We used a total of 2 + 5 = 7 steps.
It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "night", t = "thing"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The given strings are already anagrams of each other. Thus, we do not need any further steps.
</pre>



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



<ul><li><code>1 &lt;= s.length, t.length &lt;= 2 * 10<sup>5</sup></code></li><li><code>s</code>&nbsp;and&nbsp;<code>t</code>&nbsp;consist of lowercase English letters.</li></ul>



<h2><strong>Solution: Hashtable</strong></h2>



<p>Record the frequency difference of each letter.</p>



<p>Ans = sum(diff)</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minSteps(string s, string t) {
    vector&lt;int&gt; diff(26);
    for (char c : s) ++diff[c - 'a'];
    for (char c : t) --diff[c - 'a'];
    int ans = 0;
    for (int d : diff)
      ans += abs(d);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2186-minimum-number-of-steps-to-make-two-strings-anagram-ii/">花花酱 LeetCode 2186. Minimum Number of Steps to Make Two Strings Anagram 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/hashtable/leetcode-2186-minimum-number-of-steps-to-make-two-strings-anagram-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 49. Group Anagrams</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-49-group-anagrams/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-49-group-anagrams/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 02 Oct 2019 16:14:48 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[anagram]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5694</guid>

					<description><![CDATA[<p>Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] Note: All inputs&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-49-group-anagrams/">花花酱 LeetCode 49. Group Anagrams</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 an array of strings, group anagrams together.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> <code>["eat", "tea", "tan", "ate", "nat", "bat"]</code>,
<strong>Output:</strong>
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]</pre>



<p><strong>Note:</strong></p>



<ul><li>All inputs will be in lowercase.</li><li>The order of your output does not&nbsp;matter.</li></ul>



<h2><strong>Solution: HashTable</strong></h2>



<p>The sorted word will be the key of each group</p>



<p>Time complexity: O(sum(l*log(l)))<br>Space complexity: O(sum(l))</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;string&gt;&gt; groupAnagrams(vector&lt;string&gt;&amp; strs) {
    vector&lt;vector&lt;string&gt;&gt; ans;
    unordered_map&lt;string, vector&lt;int&gt;&gt; m;

    for (int i = 0; i &lt; strs.size(); ++i) {
      string c = strs[i];
      sort(begin(c), end(c));
      m[c].push_back(i);
    }

    for (const auto&amp; kv : m) {
      ans.push_back({});
      for (int i : kv.second)
        ans.back().push_back(strs[i]);    
    }

    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-49-group-anagrams/">花花酱 LeetCode 49. Group Anagrams</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-49-group-anagrams/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 438. Find All Anagrams in a String</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-438-find-all-anagrams-in-a-string/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-438-find-all-anagrams-in-a-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 28 May 2018 14:56:31 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[anagram]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[sliding]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2852</guid>

					<description><![CDATA[<p>Problem 题目大意：在s中找出所有p的Anagrams。 https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ Given a string s and a non-empty string p, find all the start indices of p&#8216;s anagrams in s. Strings consists of lowercase English letters only and the length&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-438-find-all-anagrams-in-a-string/">花花酱 LeetCode 438. Find All Anagrams in a 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/86fQQ7rVGxA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>题目大意：在s中找出所有p的Anagrams。</p>
<p><a href="https://leetcode.com/problems/find-all-anagrams-in-a-string/description/">https://leetcode.com/problems/find-all-anagrams-in-a-string/description/</a></p>
<p>Given a string <b>s</b> and a <b>non-empty</b> string <b>p</b>, find all the start indices of <b>p</b>&#8216;s anagrams in <b>s</b>.</p>
<p>Strings consists of lowercase English letters only and the length of both strings <b>s</b> and <b>p</b> will not be larger than 20,100.</p>
<p>The order of output does not matter.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b>
s: "cbaebabacd" p: "abc"

<b>Output:</b>
[0, 6]

<b>Explanation:</b>
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false "><b>Input:</b>
s: "abab" p: "ab"

<b>Output:</b>
[0, 1, 2]

<b>Explanation:</b>
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".</pre>
<h1><img class="alignnone size-full wp-image-2856" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/05/438-ep191.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/05/438-ep191.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/05/438-ep191-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/05/438-ep191-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><strong>Solution: HashTable + Sliding Window</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 35 ms
class Solution {
public:
  vector&lt;int&gt; findAnagrams(string s, string p) {
    int n = s.length();
    int l = p.length();
    vector&lt;int&gt; ans;
    vector&lt;int&gt; vp(26, 0);
    vector&lt;int&gt; vs(26, 0);
    for (char c : p) ++vp[c - 'a'];    
    for (int i = 0; i &lt; n; ++i) {
      if (i &gt;= l) --vs[s[i - l] - 'a'];        
      ++vs[s[i] - 'a'];
      if (vs == vp) ans.push_back(i + 1 - l);
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-438-find-all-anagrams-in-a-string/">花花酱 LeetCode 438. Find All Anagrams in a 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-438-find-all-anagrams-in-a-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
