<?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>count Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/count/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/count/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Nov 2020 04:30:31 +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>count Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/count/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1657. Determine if Two Strings Are Close</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1657-determine-if-two-strings-are-close/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1657-determine-if-two-strings-are-close/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Nov 2020 04:25:44 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7671</guid>

					<description><![CDATA[<p>Two strings are considered&#160;close&#160;if you can attain one from the other using the following operations: Operation 1: Swap any two&#160;existing&#160;characters. For example,&#160;abcde&#160;-&#62; aecdb Operation 2:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1657-determine-if-two-strings-are-close/">花花酱 LeetCode 1657. Determine if Two Strings Are Close</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>Two strings are considered&nbsp;<strong>close</strong>&nbsp;if you can attain one from the other using the following operations:</p>



<ul><li>Operation 1: Swap any two&nbsp;<strong>existing</strong>&nbsp;characters.<ul><li>For example,&nbsp;<code>abcde&nbsp;-&gt; aecdb</code></li></ul></li><li>Operation 2: Transform&nbsp;<strong>every</strong>&nbsp;occurrence of one&nbsp;<strong>existing</strong>&nbsp;character into another&nbsp;<strong>existing</strong>&nbsp;character, and do the same with the other character.<ul><li>For example,&nbsp;<code>aacabb&nbsp;-&gt;&nbsp;bbcbaa</code>&nbsp;(all&nbsp;<code>a</code>&#8216;s turn into&nbsp;<code>b</code>&#8216;s, and all&nbsp;<code>b</code>&#8216;s turn into&nbsp;<code>a</code>&#8216;s)</li></ul></li></ul>



<p>You can use the operations on either string as many times as necessary.</p>



<p>Given two strings,&nbsp;<code>word1</code>&nbsp;and&nbsp;<code>word2</code>, return&nbsp;<code>true</code><em>&nbsp;if&nbsp;</em><code>word1</code><em>&nbsp;and&nbsp;</em><code>word2</code><em>&nbsp;are&nbsp;<strong>close</strong>, and&nbsp;</em><code>false</code><em>&nbsp;otherwise.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word1 = "abc", word2 = "bca"
<strong>Output:</strong> true
<strong>Explanation:</strong> You can attain word2 from word1 in 2 operations.
Apply Operation 1: "abc" -&gt; "acb"
Apply Operation 1: "acb" -&gt; "bca"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word1 = "a", word2 = "aa"
<strong>Output:</strong> false
<strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word1 = "cabbba", word2 = "abbccc"
<strong>Output:</strong> true
<strong>Explanation:</strong> You can attain word2 from word1 in 3 operations.
Apply Operation 1: "cabbba" -&gt; "caabbb"
<code>Apply Operation 2: "</code>caabbb" -&gt; "baaccc"
Apply Operation 2: "baaccc" -&gt; "abbccc"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word1 = "cabbba", word2 = "aabbss"
<strong>Output:</strong> false
<strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any amount of operations.
</pre>



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



<ul><li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li><li><code>word1</code>&nbsp;and&nbsp;<code>word2</code>&nbsp;contain&nbsp;only lowercase English letters.</li></ul>



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



<p>Two strings are close:<br>1. Have the same length, ccabbb => 6 == aabccc => 6<br>2. Have the same char set, ccabbb => (a, b, c) == aabccc => (a, b, c)<br>3. Have the same sorted char counts ccabbb => (1, 2, 3) == aabccc => (1, 2, 3)</p>



<p>Time complexity: O(n)<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 closeStrings(string word1, string word2) {
    const int l1 = word1.length();
    const int l2 = word2.length();
    if (l1 != l2) return false;
    vector&lt;int&gt; f1(128), f2(128);
    vector&lt;int&gt; s1(128), s2(128);
    for (char c: word1) ++f1[c], s1[c] = 1;
    for (char c: word2) ++f2[c], s2[c] = 1;
    sort(begin(f1), end(f1));
    sort(begin(f2), end(f2));
    return f1 == f2 &amp;&amp; s1 == s2;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def closeStrings(self, word1: str, word2: str) -&gt; bool:   
    c1, c2 = Counter(word1), Counter(word2)
    return all([len(word1) == len(word2), 
                c1.keys() == c2.keys(),
                sorted(c1.values()) == sorted(c2.values())])</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1657-determine-if-two-strings-are-close/">花花酱 LeetCode 1657. Determine if Two Strings Are Close</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-1657-determine-if-two-strings-are-close/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1460. Make Two Arrays Equal by Reversing Sub-arrays</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1460-make-two-arrays-equal-by-reversing-sub-arrays/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1460-make-two-arrays-equal-by-reversing-sub-arrays/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 30 May 2020 23:09:01 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6833</guid>

					<description><![CDATA[<p>Given two integer arrays of equal length&#160;target&#160;and&#160;arr. In one step, you can select any&#160;non-empty sub-array&#160;of&#160;arr&#160;and reverse it. You are allowed to make any number of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1460-make-two-arrays-equal-by-reversing-sub-arrays/">花花酱 LeetCode 1460. Make Two Arrays Equal by Reversing Sub-arrays</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 two integer arrays of equal length&nbsp;<code>target</code>&nbsp;and&nbsp;<code>arr</code>.</p>



<p>In one step, you can select any&nbsp;<strong>non-empty sub-array</strong>&nbsp;of&nbsp;<code>arr</code>&nbsp;and reverse it. You are allowed to make any number of steps.</p>



<p>Return&nbsp;<em>True</em>&nbsp;if you can make&nbsp;<code>arr</code>&nbsp;equal to&nbsp;<code>target</code>, or&nbsp;<em>False</em>&nbsp;otherwise.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [1,2,3,4], arr = [2,4,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> You can follow the next steps to convert arr to target:
1- Reverse sub-array [2,4,1], arr becomes [1,4,2,3]
2- Reverse sub-array [4,2], arr becomes [1,2,4,3]
3- Reverse sub-array [4,3], arr becomes [1,2,3,4]
There are multiple ways to convert arr to target, this is not the only way to do so.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [7], arr = [7]
<strong>Output:</strong> true
<strong>Explanation:</strong> arr is equal to target without any reverses.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [1,12], arr = [12,1]
<strong>Output:</strong> true
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [3,7,9], arr = [3,7,11]
<strong>Output:</strong> false
<strong>Explanation:</strong> arr doesn't have value 9 and it can never be converted to target.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [1,1,1,1,1], arr = [1,1,1,1,1]
<strong>Output:</strong> true
</pre>



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



<ul><li><code>target.length == arr.length</code></li><li><code>1 &lt;= target.length &lt;= 1000</code></li><li><code>1 &lt;= target[i] &lt;= 1000</code></li><li><code>1 &lt;= arr[i] &lt;= 1000</code></li></ul>



<h2><strong>Solution: Counting</strong></h2>



<p>target and arr must have same elements.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool canBeEqual(vector&lt;int&gt;&amp; target, vector&lt;int&gt;&amp; arr) {
    vector&lt;int&gt; t(1001);
    vector&lt;int&gt; a(1001);
    for (int i = 0; i &lt; target.size(); ++i) {
      ++t[target[i]];
      ++a[arr[i]];
    }
    return t == a;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def canBeEqual(self, target: List[int], arr: List[int]) -&gt; bool:
    return Counter(target) == Counter(arr)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1460-make-two-arrays-equal-by-reversing-sub-arrays/">花花酱 LeetCode 1460. Make Two Arrays Equal by Reversing Sub-arrays</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-1460-make-two-arrays-equal-by-reversing-sub-arrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 830. Positions of Large Groups</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-830-positions-of-large-groups/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-830-positions-of-large-groups/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 May 2018 17:28:51 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2832</guid>

					<description><![CDATA[<p>Problem In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy". Call&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-830-positions-of-large-groups/">花花酱 LeetCode 830. Positions of Large Groups</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>In a string <code>S</code> of lowercase letters, these letters form consecutive groups of the same character.</p>
<p>For example, a string like <code>S = "abbxxxxzyy"</code> has the groups <code>"a"</code>, <code>"bb"</code>, <code>"xxxx"</code>, <code>"z"</code> and <code>"yy"</code>.</p>
<p>Call a group <em>large</em> if it has 3 or more characters.  We would like the starting and ending positions of every large group.</p>
<p>The final answer should be in lexicographic order.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>"abbxxxxzzy"
<strong>Output: </strong>[[3,6]]
<strong>Explanation</strong>: <code>"xxxx" is the single </code>large group with starting 3 and ending positions 6.</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>"abc"
<strong>Output: </strong>[]
<strong>Explanation</strong>: We have "a","b" and "c" but no large group.
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>"abcdddeeeeaabbbcd"
<strong>Output: </strong>[[3,5],[6,9],[12,14]]</pre>
<p>&nbsp;</p>
<p><strong>Note: </strong> <code>1 &lt;= S.length &lt;= 1000</code></p>
<h1><strong>Solution: Brute Force</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: 18 ms
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; largeGroupPositions(string S) {
    constexpr int kLargeGroupSize = 3;
    const int n = S.size();
    vector&lt;vector&lt;int&gt;&gt; ans;
    int c = 0;
    for (int i = 0; i &lt;= n; ++i, ++c) {
      if (i == n || S[i] != S[i - 1]) {
        if (c &gt;= kLargeGroupSize) ans.push_back({i - c, i - 1});
        c = 0;
      }
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-830-positions-of-large-groups/">花花酱 LeetCode 830. Positions of Large Groups</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-830-positions-of-large-groups/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 596. Classes More Than 5 Students</title>
		<link>https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/</link>
					<comments>https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 03 Apr 2018 05:55:48 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[distinct]]></category>
		<category><![CDATA[having]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2410</guid>

					<description><![CDATA[<p>Problem There is a table courses with columns: student and class Please list out all classes which have more than or equal to 5 students. For example, the table: +---------+------------+&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/">花花酱 LeetCode 596. Classes More Than 5 Students</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>There is a table <code>courses</code> with columns: <b>student</b> and <b>class</b></p>
<p>Please list out all classes which have more than or equal to 5 students.</p>
<p>For example, the table:</p>
<pre class="crayon:false">+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+
</pre>
<p>Should output:</p>
<pre class="crayon:false ">+---------+
| class   |
+---------+
| Math    |
+---------+
</pre>
<p><b>Note:</b><br />
The students should not be counted duplicate in each course.</p>
<h1><strong>Solution</strong></h1>
<p>SQL</p><pre class="crayon-plain-tag"># Author: Huahua
# Running time: 1756 ms
select class from courses group by class having count(distinct student) &gt;= 5</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sql/leetcode-596-classes-more-than-5-students/">花花酱 LeetCode 596. Classes More Than 5 Students</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/sql/leetcode-596-classes-more-than-5-students/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
