<?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>counter Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/counter/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/counter/</link>
	<description></description>
	<lastBuildDate>Fri, 28 Apr 2023 05:02:07 +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>counter Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/counter/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2653. Sliding Subarray Beauty</title>
		<link>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2653-sliding-subarray-beauty/</link>
					<comments>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2653-sliding-subarray-beauty/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 28 Apr 2023 04:43:28 +0000</pubDate>
				<category><![CDATA[Sliding Window]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sliding window]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9996</guid>

					<description><![CDATA[<p>Given an integer array&#160;nums&#160;containing&#160;n&#160;integers, find the&#160;beauty&#160;of each subarray of size&#160;k. The&#160;beauty&#160;of a subarray is the&#160;xth&#160;smallest integer&#160;in the subarray if it is&#160;negative, or&#160;0&#160;if there are fewer&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-2653-sliding-subarray-beauty/">花花酱 LeetCode 2653. Sliding Subarray Beauty</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 integer array&nbsp;<code>nums</code>&nbsp;containing&nbsp;<code>n</code>&nbsp;integers, find the&nbsp;<strong>beauty</strong>&nbsp;of each subarray of size&nbsp;<code>k</code>.</p>



<p>The&nbsp;<strong>beauty</strong>&nbsp;of a subarray is the&nbsp;<code>x<sup>th</sup></code><strong>&nbsp;smallest integer&nbsp;</strong>in the subarray if it is&nbsp;<strong>negative</strong>, or&nbsp;<code>0</code>&nbsp;if there are fewer than&nbsp;<code>x</code>&nbsp;negative integers.</p>



<p>Return&nbsp;<em>an integer array containing&nbsp;</em><code>n - k + 1</code>&nbsp;<em>integers, which denote the&nbsp;</em><strong>beauty</strong><em>&nbsp;of the subarrays&nbsp;<strong>in order</strong>&nbsp;from the first index in the array.</em></p>



<ul><li>A subarray is a contiguous&nbsp;<strong>non-empty</strong>&nbsp;sequence of elements within an array.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,-1,-3,-2,3], k = 3, x = 2
<strong>Output:</strong> [-1,-2,-2]
<strong>Explanation:</strong> There are 3 subarrays with size k = 3. 
The first subarray is <code>[1, -1, -3]</code> and the 2<sup>nd</sup> smallest negative integer is -1.&nbsp;
The second subarray is <code>[-1, -3, -2]</code> and the 2<sup>nd</sup> smallest negative integer is -2.&nbsp;
The third subarray is <code>[-3, -2, 3]&nbsp;</code>and the 2<sup>nd</sup> smallest negative integer is -2.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,-2,-3,-4,-5], k = 2, x = 2
<strong>Output:</strong> [-1,-2,-3,-4]
<strong>Explanation:</strong> There are 4 subarrays with size k = 2.
For <code>[-1, -2]</code>, the 2<sup>nd</sup> smallest negative integer is -1.
For <code>[-2, -3]</code>, the 2<sup>nd</sup> smallest negative integer is -2.
For <code>[-3, -4]</code>, the 2<sup>nd</sup> smallest negative integer is -3.
For <code>[-4, -5]</code>, the 2<sup>nd</sup> smallest negative integer is -4.&nbsp;</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-3,1,2,-3,0,-3], k = 2, x = 1
<strong>Output:</strong> [-3,0,-3,-3,-3]
<strong>Explanation:</strong> There are 5 subarrays with size k = 2<strong>.</strong>
For <code>[-3, 1]</code>, the 1<sup>st</sup> smallest negative integer is -3.
For <code>[1, 2]</code>, there is no negative integer so the beauty is 0.
For <code>[2, -3]</code>, the 1<sup>st</sup> smallest negative integer is -3.
For <code>[-3, 0]</code>, the 1<sup>st</sup> smallest negative integer is -3.
For <code>[0, -3]</code>, the 1<sup>st</sup> smallest negative integer is -3.</pre>



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



<ul><li><code>n == nums.length&nbsp;</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= k &lt;= n</code></li><li><code>1 &lt;= x &lt;= k&nbsp;</code></li><li><code>-50&nbsp;&lt;= nums[i] &lt;= 50&nbsp;</code></li></ul>



<h2><strong>Solution: Sliding Window + Counter</strong></h2>



<p>Since the range of nums are very small (-50 ~ 50), we can use a counter to track the frequency of each element, s.t. we can find the k-the smallest element in O(50) instead of O(k).</p>



<p>Time complexity: O((n &#8211; k + 1) * 50)<br>Space complexity: O(50)</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;int&gt; getSubarrayBeauty(vector&lt;int&gt;&amp; nums, int k, int x) {
    constexpr int kMax = 50;    
    const int n = nums.size();
    vector&lt;int&gt; counts(2 * kMax + 1);
    vector&lt;int&gt; ans;    
    for (int i = 0; i &lt; n; ++i) {
      if (i &gt;= k)
        --counts[nums[i - k] + kMax];
      ++counts[nums[i] + kMax];      
      if (i &gt;= k - 1) {
        int b = 0;
        for (int j = 0, s = 0; j &lt;= kMax; ++j) {
          s += counts[j];
          if (s &gt;= x) {
            b = j - kMax;
            break;
          }
        }
        ans.push_back(b);
      }
     
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-2653-sliding-subarray-beauty/">花花酱 LeetCode 2653. Sliding Subarray Beauty</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/sliding-window/leetcode-2653-sliding-subarray-beauty/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2206. Divide Array Into Equal Pairs</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2206-divide-array-into-equal-pairs/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2206-divide-array-into-equal-pairs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 21 Mar 2022 11:25:29 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9577</guid>

					<description><![CDATA[<p>You are given an integer array&#160;nums&#160;consisting of&#160;2 * n&#160;integers. You need to divide&#160;nums&#160;into&#160;n&#160;pairs such that: Each element belongs to&#160;exactly one&#160;pair. The elements present in a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2206-divide-array-into-equal-pairs/">花花酱 LeetCode 2206. Divide Array Into Equal Pairs</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 an integer array&nbsp;<code>nums</code>&nbsp;consisting of&nbsp;<code>2 * n</code>&nbsp;integers.</p>



<p>You need to divide&nbsp;<code>nums</code>&nbsp;into&nbsp;<code>n</code>&nbsp;pairs such that:</p>



<ul><li>Each element belongs to&nbsp;<strong>exactly one</strong>&nbsp;pair.</li><li>The elements present in a pair are&nbsp;<strong>equal</strong>.</li></ul>



<p>Return&nbsp;<code>true</code>&nbsp;<em>if nums can be divided into</em>&nbsp;<code>n</code>&nbsp;<em>pairs, otherwise return</em>&nbsp;<code>false</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,2,3,2,2,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> 
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> false
<strong>Explanation:</strong> 
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
</pre>



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



<ul><li><code>nums.length == 2 * n</code></li><li><code>1 &lt;= n &lt;= 500</code></li><li><code>1 &lt;= nums[i] &lt;= 500</code></li></ul>



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



<p>Each number has to appear even numbers in order to be paired. Count the frequency of each number, return true if all of them are even numbers, return false otherwise. </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:
  bool divideArray(vector&lt;int&gt;&amp; nums) {
    unordered_map&lt;int, int&gt; m;
    for (int num : nums)
      ++m[num];
    return all_of(begin(m), end(m), [](const auto&amp; e) { return e.second % 2 == 0; });    
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2206-divide-array-into-equal-pairs/">花花酱 LeetCode 2206. Divide Array Into Equal Pairs</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-2206-divide-array-into-equal-pairs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2150. Find All Lonely Numbers in the Array</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2150-find-all-lonely-numbers-in-the-array/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2150-find-all-lonely-numbers-in-the-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Feb 2022 01:13:01 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9474</guid>

					<description><![CDATA[<p>You are given an integer array&#160;nums. A number&#160;x&#160;is&#160;lonely&#160;when it appears only&#160;once, and no&#160;adjacent&#160;numbers (i.e.&#160;x + 1&#160;and&#160;x - 1)&#160;appear in the array. Return&#160;all&#160;lonely numbers in&#160;nums. You&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2150-find-all-lonely-numbers-in-the-array/">花花酱 LeetCode 2150. Find All Lonely Numbers in the 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 an integer array&nbsp;<code>nums</code>. A number&nbsp;<code>x</code>&nbsp;is&nbsp;<strong>lonely</strong>&nbsp;when it appears only&nbsp;<strong>once</strong>, and no&nbsp;<strong>adjacent</strong>&nbsp;numbers (i.e.&nbsp;<code>x + 1</code>&nbsp;and&nbsp;<code>x - 1)</code>&nbsp;appear in the array.</p>



<p>Return&nbsp;<em><strong>all</strong>&nbsp;lonely numbers in&nbsp;</em><code>nums</code>. You may return the answer in&nbsp;<strong>any order</strong>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [10,6,5,8]
<strong>Output:</strong> [10,8]
<strong>Explanation:</strong> 
- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
- 5 is not a lonely number since 6 appears in nums and vice versa.
Hence, the lonely numbers in nums are [10, 8].
Note that [8, 10] may also be returned.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,3,5,3]
<strong>Output:</strong> [1,5]
<strong>Explanation:</strong> 
- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
- 3 is not a lonely number since it appears twice.
Hence, the lonely numbers in nums are [1, 5].
Note that [5, 1] may also be returned.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li></ul>



<h2><strong>Solution: Counter</strong></h2>



<p>Computer the frequency of each number in the array, for a given number x with freq = 1, check freq of (x &#8211; 1) and (x + 1), if both of them are zero then x is lonely.</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:
  vector&lt;int&gt; findLonely(vector&lt;int&gt;&amp; nums) {
    unordered_map&lt;int, int&gt; m;
    for (int x : nums)
      ++m[x];
    vector&lt;int&gt; ans;
    for (const auto [x, c] : m)
      if (c == 1 &amp;&amp; !m.count(x + 1) &amp;&amp; !m.count(x - 1))
        ans.push_back(x);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2150-find-all-lonely-numbers-in-the-array/">花花酱 LeetCode 2150. Find All Lonely Numbers in the 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/hashtable/leetcode-2150-find-all-lonely-numbers-in-the-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1737. Change Minimum Characters to Satisfy One of Three Conditions</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1737-change-minimum-characters-to-satisfy-one-of-three-conditions/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1737-change-minimum-characters-to-satisfy-one-of-three-conditions/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Jan 2021 05:58:04 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8025</guid>

					<description><![CDATA[<p>You are given two strings&#160;a&#160;and&#160;b&#160;that consist of lowercase letters. In one operation, you can change any character in&#160;a&#160;or&#160;b&#160;to&#160;any lowercase letter. Your goal is to satisfy&#160;one&#160;of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1737-change-minimum-characters-to-satisfy-one-of-three-conditions/">花花酱 LeetCode 1737. Change Minimum Characters to Satisfy One of Three Conditions</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>a</code>&nbsp;and&nbsp;<code>b</code>&nbsp;that consist of lowercase letters. In one operation, you can change any character in&nbsp;<code>a</code>&nbsp;or&nbsp;<code>b</code>&nbsp;to&nbsp;<strong>any lowercase letter</strong>.</p>



<p>Your goal is to satisfy&nbsp;<strong>one</strong>&nbsp;of the following three conditions:</p>



<ul><li><strong>Every</strong>&nbsp;letter in&nbsp;<code>a</code>&nbsp;is&nbsp;<strong>strictly less</strong>&nbsp;than&nbsp;<strong>every</strong>&nbsp;letter in&nbsp;<code>b</code>&nbsp;in the alphabet.</li><li><strong>Every</strong>&nbsp;letter in&nbsp;<code>b</code>&nbsp;is&nbsp;<strong>strictly less</strong>&nbsp;than&nbsp;<strong>every</strong>&nbsp;letter in&nbsp;<code>a</code>&nbsp;in the alphabet.</li><li><strong>Both</strong>&nbsp;<code>a</code>&nbsp;and&nbsp;<code>b</code>&nbsp;consist of&nbsp;<strong>only one</strong>&nbsp;distinct letter.</li></ul>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of operations needed to achieve your goal.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> a = "aba", b = "caa"
<strong>Output:</strong> 2
<strong>Explanation:</strong> Consider the best way to make each condition true:
1) Change b to "ccc" in 2 operations, then every letter in a is less than every letter in b.
2) Change a to "bbb" and b to "aaa" in 3 operations, then every letter in b is less than every letter in a.
3) Change a to "aaa" and b to "aaa" in 2 operations, then a and b consist of one distinct letter.
The best way was done in 2 operations (either condition 1 or condition 3).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> a = "dabadd", b = "cda"
<strong>Output:</strong> 3
<strong>Explanation:</strong> The best way is to make condition 1 true by changing b to "eee".
</pre>



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



<ul><li><code>1 &lt;= a.length, b.length &lt;= 10<sup>5</sup></code></li><li><code>a</code>&nbsp;and&nbsp;<code>b</code>&nbsp;consist only of lowercase letters.</li></ul>



<p></p>



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



<p>Time complexity: O(26*(m+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:
  int minCharacters(string a, string b) {
    auto change = [](const string&amp; a, const string&amp; b) {
      int ans = INT_MAX; 
      for (char c = 'a'; c &lt; 'z'; ++c) { 
        int ops = 0;
        for (char x : a) ops += x &gt; c; 
        for (char x : b) ops += x &lt;= c; 
        ans = min(ans, ops);
      }
      return ans;
    };        
    int ans = min(change(a, b), change(b, a));
    for (char c = 'a'; c &lt;= 'z'; ++c) {
      int ops = a.size() - count(begin(a), end(a), c) + 
         b.size() - count(begin(b), end(b), c);              
      ans = min(ans, ops);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1737-change-minimum-characters-to-satisfy-one-of-three-conditions/">花花酱 LeetCode 1737. Change Minimum Characters to Satisfy One of Three Conditions</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-1737-change-minimum-characters-to-satisfy-one-of-three-conditions/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 383. Ransom Note</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-383-ransom-note/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-383-ransom-note/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 16 Mar 2018 06:19:15 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[counting]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2114</guid>

					<description><![CDATA[<p>题目大意：给你一个字符串，问能否用它其中的字符组成另外一个字符串，每个字符只能使用一次。 Problem: Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-383-ransom-note/">花花酱 LeetCode 383. Ransom Note</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>题目大意：给你一个字符串，问能否用它其中的字符组成另外一个字符串，每个字符只能使用一次。</p>
<p><strong>Problem:</strong></p>
<p>Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.</p>
<p>Each letter in the magazine string can only be used once in your ransom note.</p>
<p><b>Note:</b><br />
You may assume that both strings contain only lowercase letters.</p>
<pre class="crayon:false">canConstruct("a", "b") -&gt; false
canConstruct("aa", "ab") -&gt; false
canConstruct("aa", "aab") -&gt; true</pre>
<p><strong>Solution: HashTable</strong></p>
<p>Time complexity: O(n + m)</p>
<p>Space complexity: O(128)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 24 ms
class Solution {
public:
  bool canConstruct(string ransomNote, string magazine) {
    vector&lt;int&gt; counts(128, 0);
    for (const char c : magazine)
      ++counts[c];
    for (const char c : ransomNote)
      if (--counts[c] &lt; 0) return false;
    return true;
  }
};</pre><p></p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 9 ms (beats 100%)

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();

class Solution {
public:
  bool canConstruct(const string&amp; ransomNote, const string&amp; magazine) {
    vector&lt;int&gt; counts(128, 0);
    for (const char c : magazine)
      ++counts[c];
    for (const char c : ransomNote)
      if (--counts[c] &lt; 0) return false;
    return true;
  }
};</pre><p>&nbsp;</p>
<p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 64 ms
"""
class Solution:
  def canConstruct(self, ransomNote, magazine):
    return not collections.Counter(ransomNote) - collections.Counter(magazine);</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-383-ransom-note/">花花酱 LeetCode 383. Ransom Note</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-383-ransom-note/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 771. Jewels and Stones</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-771-jewels-and-stones/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-771-jewels-and-stones/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 02 Feb 2018 16:25:34 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[counter]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1722</guid>

					<description><![CDATA[<p>题目大意：给你宝石的类型，再给你一堆石头，返回里面宝石的数量。 Problem: You&#8217;re given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-771-jewels-and-stones/">花花酱 LeetCode 771. Jewels and Stones</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>题目大意：给你宝石的类型，再给你一堆石头，返回里面宝石的数量。</p>
<h1><strong>Problem:</strong></h1>
<p>You&#8217;re given strings <code>J</code> representing the types of stones that are jewels, and <code>S</code> representing the stones you have.  Each character in <code>S</code> is a type of stone you have.  You want to know how many of the stones you have are also jewels.</p>
<p>The letters in <code>J</code> are guaranteed distinct, and all characters in <code>J</code> and <code>S</code> are letters. Letters are case sensitive, so <code>"a"</code> is considered a different type of stone from <code>"A"</code>.</p>
<h2><strong>Example 1:</strong></h2>
<pre class="crayon:false">Input: J = "aA", S = "aAAbbbb"
Output: 3
</pre>
<h2><strong>Example 2:</strong></h2>
<pre class="crayon:false ">Input: J = "z", S = "ZZ"
Output: 0
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>S</code> and <code>J</code> will consist of letters and have length at most 50.</li>
<li>The characters in <code>J</code> are distinct.</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><strong>Solution 1: HashTable</strong></h1>
<p>Time complexity: O(|J| + |S|)</p>
<p>Space complexity: O(128) / O(|J|)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 9 ms
class Solution {
public:
  int numJewelsInStones(string J, string S) {
    std::vector&lt;int&gt; f(128, 0);
    int ans = 0;
    for (const char j : J)
      f[j] = 1;
    for (const char s : S)
      ans += f[s] &amp; 1;
    return ans;
  }
};</pre><p></div><h2 class="tabtitle">C++ v2</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 10 ms
class Solution {
public:
  int numJewelsInStones(const string&amp; J, const string&amp; S) {
    std::set&lt;char&gt; f(J.begin(), J.end());
    return std::count_if(S.begin(), S.end(), 
                         [&amp;f](const char c) { return f.count(c); });
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 18 ms
class Solution {
  public int numJewelsInStones(String J, String S) {
    int[] f = new int[128];
    for (final char c : J.toCharArray())
      f[c] = 1;
    int ans = 0;
    for (final char c : S.toCharArray())
      ans += f[c];
    return ans;
  }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 43 ms
"""
class Solution(object):
  def numJewelsInStones(self, J, S):
    f = set(J)
    return sum([s in f for s in S])</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-771-jewels-and-stones/">花花酱 LeetCode 771. Jewels and Stones</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-771-jewels-and-stones/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
