<?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>hashset Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/hashset/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/hashset/</link>
	<description></description>
	<lastBuildDate>Sat, 05 Feb 2022 03:15:13 +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>hashset Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/hashset/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2154. Keep Multiplying Found Values by Two</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2154-keep-multiplying-found-values-by-two/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2154-keep-multiplying-found-values-by-two/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Feb 2022 03:14:44 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashset]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9481</guid>

					<description><![CDATA[<p>You are given an array of integers&#160;nums. You are also given an integer&#160;original&#160;which is the first number that needs to be searched for in&#160;nums. You&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2154-keep-multiplying-found-values-by-two/">花花酱 LeetCode 2154. Keep Multiplying Found Values by Two</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 array of integers&nbsp;<code>nums</code>. You are also given an integer&nbsp;<code>original</code>&nbsp;which is the first number that needs to be searched for in&nbsp;<code>nums</code>.</p>



<p>You then do the following steps:</p>



<ol><li>If&nbsp;<code>original</code>&nbsp;is found in&nbsp;<code>nums</code>,&nbsp;<strong>multiply</strong>&nbsp;it by two (i.e., set&nbsp;<code>original = 2 * original</code>).</li><li>Otherwise,&nbsp;<strong>stop</strong>&nbsp;the process.</li><li><strong>Repeat</strong>&nbsp;this process with the new number as long as you keep finding the number.</li></ol>



<p>Return&nbsp;<em>the&nbsp;<strong>final</strong>&nbsp;value of&nbsp;</em><code>original</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,3,6,1,12], original = 3
<strong>Output:</strong> 24
<strong>Explanation:</strong> 
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
- 24 is not found in nums. Thus, 24 is returned.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,7,9], original = 4
<strong>Output:</strong> 4
<strong>Explanation:</strong>
- 4 is not found in nums. Thus, 4 is returned.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i], original &lt;= 1000</code></li></ul>



<h2><strong>Solution: Hashset</strong></h2>



<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:
  int findFinalValue(vector&lt;int&gt;&amp; nums, int original) {
    unordered_set&lt;int&gt; s(begin(nums), end(nums));
    while (true) {
      if (!s.count(original)) break;
      original *= 2;
    }
    return original;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2154-keep-multiplying-found-values-by-two/">花花酱 LeetCode 2154. Keep Multiplying Found Values by Two</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-2154-keep-multiplying-found-values-by-two/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2103. Rings and Rods</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2103-rings-and-rods/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2103-rings-and-rods/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Dec 2021 23:08:22 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashset]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9157</guid>

					<description><![CDATA[<p>Problem Solution: Hashset Use 10 hashsets to track the status of each rod, check whether it contains three unique elements (R,G,B). Time complexity: O(n)Space complexity:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2103-rings-and-rods/">花花酱 LeetCode 2103. Rings and Rods</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><a href="https://leetcode.com/problems/rings-and-rods/">Problem</a></p>



<h2><strong>Solution: Hashset</strong></h2>



<p>Use 10 hashsets to track the status of each rod, check whether it contains three unique elements (R,G,B).</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int countPoints(string rings) {
    constexpr int kRods = 10;
    vector&lt;unordered_set&lt;char&gt;&gt; s(kRods);
    for (int i = 0; i &lt; rings.size(); i += 2)      
      s[rings[i + 1] - '0'].insert(rings[i]);
    int ans = 0;
    for (int i = 0; i &lt; kRods; ++i)
      if (s[i].size() == 3)
        ++ans;
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2103-rings-and-rods/">花花酱 LeetCode 2103. Rings and Rods</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-2103-rings-and-rods/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1876. Substrings of Size Three with Distinct Characters</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1876-substrings-of-size-three-with-distinct-characters/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1876-substrings-of-size-three-with-distinct-characters/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 07 Aug 2021 06:16:41 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashset]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8499</guid>

					<description><![CDATA[<p>A string is&#160;good&#160;if there are no repeated characters. Given a string&#160;s​​​​​, return&#160;the number of&#160;good substrings&#160;of length&#160;three&#160;in&#160;s​​​​​​. Note that if there are multiple occurrences of the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1876-substrings-of-size-three-with-distinct-characters/">花花酱 LeetCode 1876. Substrings of Size Three with Distinct Characters</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>A string is&nbsp;<strong>good</strong>&nbsp;if there are no repeated characters.</p>



<p>Given a string&nbsp;<code>s</code>​​​​​, return&nbsp;<em>the number of&nbsp;<strong>good substrings</strong>&nbsp;of length&nbsp;<strong>three&nbsp;</strong>in&nbsp;</em><code>s</code>​​​​​​.</p>



<p>Note that if there are multiple occurrences of the same substring, every occurrence should be counted.</p>



<p>A&nbsp;<strong>substring</strong>&nbsp;is a contiguous sequence of characters in a string.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "xyzzaz"
<strong>Output:</strong> 1
<strong>Explanation:</strong> There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". 
The only good substring of length 3 is "xyz".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "aababcabc"
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc".
The good substrings are "abc", "bca", "cab", and "abc".
</pre>



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



<ul><li><code>1 &lt;= s.length &lt;= 100</code></li><li><code>s</code>​​​​​​ consists of lowercase English letters.</li></ul>



<h2><strong>Solution: Brute Force w/ (Hash)Set</strong></h2>



<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">class Solution {
public:
  int countGoodSubstrings(string s) {
    int ans = 0;
    for (int i = 0; i + 2 &lt; s.length(); ++i) 
      ans += set&lt;char&gt;(s.begin() + i, s.begin() + i + 3).size() == 3;
    return ans;
  }
};</pre>

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

<pre class="crayon-plain-tag">class Solution:
  def countGoodSubstrings(self, s: str) -&gt; int:    
    return sum(len(set(s[i:i + 3])) == 3 for i in range(len(s) - 2))</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1876-substrings-of-size-three-with-distinct-characters/">花花酱 LeetCode 1876. Substrings of Size Three with Distinct Characters</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-1876-substrings-of-size-three-with-distinct-characters/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1817. Finding the Users Active Minutes</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1817-finding-the-users-active-minutes/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1817-finding-the-users-active-minutes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 07 Apr 2021 00:05:02 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[counting]]></category>
		<category><![CDATA[hashset]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8323</guid>

					<description><![CDATA[<p>You are given the logs for users&#8217; actions on LeetCode, and an integer&#160;k. The logs are represented by a 2D integer array&#160;logs&#160;where each&#160;logs[i] = [IDi,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1817-finding-the-users-active-minutes/">花花酱 LeetCode 1817. Finding the Users Active Minutes</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 the logs for users&#8217; actions on LeetCode, and an integer&nbsp;<code>k</code>. The logs are represented by a 2D integer array&nbsp;<code>logs</code>&nbsp;where each&nbsp;<code>logs[i] = [ID<sub>i</sub>, time<sub>i</sub>]</code>&nbsp;indicates that the user with&nbsp;<code>ID<sub>i</sub></code>&nbsp;performed an action at the minute&nbsp;<code>time<sub>i</sub></code>.</p>



<p><strong>Multiple users</strong>&nbsp;can perform actions simultaneously, and a single user can perform&nbsp;<strong>multiple actions</strong>&nbsp;in the same minute.</p>



<p>The&nbsp;<strong>user active minutes (UAM)</strong>&nbsp;for a given user is defined as the&nbsp;<strong>number of unique minutes</strong>&nbsp;in which the user performed an action on LeetCode. A minute can only be counted once, even if multiple actions occur during it.</p>



<p>You are to calculate a&nbsp;<strong>1-indexed</strong>&nbsp;array&nbsp;<code>answer</code>&nbsp;of size&nbsp;<code>k</code>&nbsp;such that, for each&nbsp;<code>j</code>&nbsp;(<code>1 &lt;= j &lt;= k</code>),&nbsp;<code>answer[j]</code>&nbsp;is the&nbsp;<strong>number of users</strong>&nbsp;whose&nbsp;<strong>UAM</strong>&nbsp;equals&nbsp;<code>j</code>.</p>



<p>Return&nbsp;<em>the array&nbsp;</em><code>answer</code><em>&nbsp;as described above</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5
<strong>Output:</strong> [0,2,0,0,0]
<strong>Explanation:</strong>
The user with ID=0 performed actions at minutes 5, 2, and 5 again. Hence, they have a UAM of 2 (minute 5 is only counted once).
The user with ID=1 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.
Since both users have a UAM of 2, answer[2] is 2, and the remaining answer[j] values are 0.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> logs = [[1,1],[2,2],[2,3]], k = 4
<strong>Output:</strong> [1,1,0,0]
<strong>Explanation:</strong>
The user with ID=1 performed a single action at minute 1. Hence, they have a UAM of 1.
The user with ID=2 performed actions at minutes 2 and 3. Hence, they have a UAM of 2.
There is one user with a UAM of 1 and one with a UAM of 2.
Hence, answer[1] = 1, answer[2] = 1, and the remaining values are 0.
</pre>



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



<ul><li><code>1 &lt;= logs.length &lt;= 10<sup>4</sup></code></li><li><code>0 &lt;= ID<sub>i</sub>&nbsp;&lt;= 10<sup>9</sup></code></li><li><code>1 &lt;= time<sub>i</sub>&nbsp;&lt;= 10<sup>5</sup></code></li><li><code>k</code>&nbsp;is in the range&nbsp;<code>[The maximum&nbsp;<strong>UAM</strong>&nbsp;for a user, 10<sup>5</sup>]</code>.</li></ul>



<h2><strong>Solution: Hashsets in a Hashtable</strong></h2>



<p>key: user_id, value: set{time} </p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  vector&lt;int&gt; findingUsersActiveMinutes(vector&lt;vector&lt;int&gt;&gt;&amp; logs, int k) {
    unordered_map&lt;int, unordered_set&lt;int&gt;&gt; m;
    vector&lt;int&gt; ans(k);
    for (const auto&amp; log : logs)
      m[log[0]].insert(log[1]);
    for (const auto&amp; [id, s] : m)
      ++ans[s.size() - 1];
    return ans;
  }
};</pre>
</div></div>



<p><br></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1817-finding-the-users-active-minutes/">花花酱 LeetCode 1817. Finding the Users Active Minutes</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-1817-finding-the-users-active-minutes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1695. Maximum Erasure Value</title>
		<link>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1695-maximum-erasure-value/</link>
					<comments>https://zxi.mytechroad.com/blog/sliding-window/leetcode-1695-maximum-erasure-value/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 20 Dec 2020 09:14:16 +0000</pubDate>
				<category><![CDATA[Sliding Window]]></category>
		<category><![CDATA[hashset]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sliding window]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7823</guid>

					<description><![CDATA[<p>You are given an array of positive integers&#160;nums&#160;and want to erase a subarray containing&#160;unique elements. The&#160;score&#160;you get by erasing the subarray is equal to the&#160;sum&#160;of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-1695-maximum-erasure-value/">花花酱 LeetCode 1695. Maximum Erasure Value</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 array of positive integers&nbsp;<code>nums</code>&nbsp;and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The&nbsp;<strong>score</strong>&nbsp;you get by erasing the subarray is equal to the&nbsp;<strong>sum</strong>&nbsp;of its elements.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>maximum score</strong>&nbsp;you can get by erasing&nbsp;<strong>exactly one</strong>&nbsp;subarray.</em></p>



<p>An array&nbsp;<code>b</code>&nbsp;is called to be a&nbsp;subarray&nbsp;of&nbsp;<code>a</code>&nbsp;if it forms a contiguous subsequence of&nbsp;<code>a</code>, that is, if it is equal to&nbsp;<code>a[l],a[l+1],...,a[r]</code>&nbsp;for some&nbsp;<code>(l,r)</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,2,4,5,6]
<strong>Output:</strong> 17
<strong>Explanation:</strong> The optimal subarray here is [2,4,5,6].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,2,1,2,5,2,1,2,5]
<strong>Output:</strong> 8
<strong>Explanation:</strong> The optimal subarray here is [5,2,1] or [1,2,5].
</pre>



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



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



<h2><strong>Solution: Sliding window + Hashset</strong></h2>



<p>Maintain a window that has no duplicate elements.</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:
  int maximumUniqueSubarray(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    unordered_set&lt;int&gt; t;
    int ans = 0;
    for (int l = 0, r = 0, s = 0; r &lt; n; ++r) {
      while (t.count(nums[r]) &amp;&amp; l &lt; r) {
        s -= nums[l];
        t.erase(nums[l++]);
      }      
      t.insert(nums[r]);
      ans = max(ans, s += nums[r]);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-1695-maximum-erasure-value/">花花酱 LeetCode 1695. Maximum Erasure Value</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-1695-maximum-erasure-value/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1316. Distinct Echo Substrings</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1316-distinct-echo-substrings/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1316-distinct-echo-substrings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 11 Jan 2020 17:34:57 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[hashset]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6065</guid>

					<description><![CDATA[<p>Return the number of&#160;distinct&#160;non-empty substrings of&#160;text&#160;that can be written as the concatenation of some string with itself. Example 1: Input: text = "abcabcabc" Output: 3&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1316-distinct-echo-substrings/">花花酱 LeetCode 1316. Distinct Echo Substrings</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>Return the number of&nbsp;<strong>distinct</strong>&nbsp;non-empty substrings of&nbsp;<code>text</code>&nbsp;that can be written as the concatenation of some string with itself.</p>



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



<pre class="wp-block-preformatted; crayon:false"><strong>Input:</strong> text = "abcabcabc"
<strong>Output:</strong> 3
<strong>Explanation: </strong>The 3 substrings are "abcabc", "bcabca" and "cabcab".
</pre>



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



<pre class="wp-block-preformatted; crayon:false"><strong>Input:</strong> text = "leetcodeleetcode"
<strong>Output:</strong> 2
<strong>Explanation: </strong>The 2 substrings are "ee" and "leetcodeleetcode".
</pre>



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



<ul><li><code>1 &lt;= text.length &lt;= 2000</code></li><li><code>text</code>&nbsp;has only lowercase English letters.</li></ul>



<h2><strong>Solution 1:  Brute Force + HashSet</strong></h2>



<p>Try all possible substrings</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int distinctEchoSubstrings(string text) {
    const int n = text.length();
    string_view t(text);
    unordered_set&lt;string_view&gt; s;    
    for (int k = 1; k &lt;= n / 2; ++k)
      for (int i = 0; i + k &lt;= n; ++i)
        if (t.substr(i, k) == t.substr(i + k, k))
          s.insert(t.substr(i, 2 * k));
    return s.size();
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1316-distinct-echo-substrings/">花花酱 LeetCode 1316. Distinct Echo Substrings</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-1316-distinct-echo-substrings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 820. Short Encoding of Words</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-820-short-encoding-of-words/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-820-short-encoding-of-words/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 24 Apr 2018 07:45:28 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[hashset]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[suffix]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2760</guid>

					<description><![CDATA[<p>Problem Given a list of words, we may encode it by writing a reference string S and a list of indexes A. For example, if the list of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-820-short-encoding-of-words/">花花酱 LeetCode 820. Short Encoding of Words</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 a list of words, we may encode it by writing a reference string <code>S</code> and a list of indexes <code>A</code>.</p>
<p>For example, if the list of words is <code>["time", "me", "bell"]</code>, we can write it as <code>S = "time#bell#"</code> and <code>indexes = [0, 2, 5]</code>.</p>
<p>Then for each index, we will recover the word by reading from the reference string from that index until we reach a &#8220;#&#8221; character.</p>
<p>What is the length of the shortest reference string S possible that encodes the given words?</p>
<p><strong>Example:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> words = <code>["time", "me", "bell"]</code> <strong>Output:</strong> 10 <strong>Explanation:</strong> S = <code>"time#bell#" and indexes = [0, 2, 5</code>].</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= words.length &lt;= 2000.</code></li>
<li><code>1 &lt;= words[i].length &lt;= 7.</code></li>
<li>Each word has only lowercase letters.</li>
</ol>
<h1><strong>Idea</strong></h1>
<p>Remove all the words that are suffix of other words.</p>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(n*l^2)</p>
<p>Space complexity: O(n*l)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 43 ms
class Solution {
public:
  int minimumLengthEncoding(vector&lt;string&gt;&amp; words) {
    unordered_set&lt;string&gt; s(words.begin(), words.end());
    for (const string&amp; w : words) {
      for (int i = w.length() - 1; i &gt; 0; --i)        
        s.erase(w.substr(i));
    }
    int ans = 0;
    for (const string&amp; w : s)
      ans += w.length() + 1;
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-820-short-encoding-of-words/">花花酱 LeetCode 820. Short Encoding of Words</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-820-short-encoding-of-words/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
