<?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>signature Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/signature/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/signature/</link>
	<description></description>
	<lastBuildDate>Thu, 30 Aug 2018 16:25: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>signature Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/signature/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 893. Groups of Special-Equivalent Strings</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-893-groups-of-special-equivalent-strings/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-893-groups-of-special-equivalent-strings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 27 Aug 2018 04:50:13 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[signature]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3714</guid>

					<description><![CDATA[<p>Problem You are given an array A of strings. Two strings S and T are special-equivalent if after any number of moves, S == T. A move consists of choosing two indices i and j with i % 2 == j&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-893-groups-of-special-equivalent-strings/">花花酱 LeetCode 893. Groups of Special-Equivalent Strings</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>You are given an array <code>A</code> of strings.</p>
<p>Two strings <code>S</code> and <code>T</code> are <em>special-equivalent</em> if after any number of <em>moves</em>, S == T.</p>
<p>A <em>move</em> consists of choosing two indices <code>i</code> and <code>j</code> with <code>i % 2 == j % 2</code>, and swapping <code>S[i]</code> with <code>S[j]</code>.</p>
<p>Now, a <em>group of special-equivalent strings from <code>A</code></em> is a non-empty subset S of <code>A</code> such that any string not in S is not special-equivalent with any string in S.</p>
<p>Return the number of groups of special-equivalent strings from <code>A</code>.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">["a","b","c","a","c","c"]</span>
<strong>Output: </strong><span id="example-output-1">3</span>
<strong>Explanation</strong>: 3 groups ["a","a"], ["b"], ["c","c","c"]
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">["aa","bb","ab","ba"]</span>
<strong>Output: </strong><span id="example-output-2">4</span>
<strong>Explanation</strong>: 4 groups <span id="example-input-2-1">["aa"], ["bb"], ["ab"], ["ba"]</span>
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">["abc","acb","bac","bca","cab","cba"]</span>
<strong>Output: </strong><span id="example-output-3">3</span>
<strong>Explanation</strong>: 3 groups ["abc","cba"], ["acb","bca"], ["bac","cab"]
</pre>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-4-1">["abcd","cdab","adcb","cbad"]</span>
<strong>Output: </strong><span id="example-output-4">1</span>
<strong>Explanation</strong>: 1 group <span id="example-input-4-1">["abcd","cdab","adcb","cbad"]</span>
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>1 &lt;= A.length &lt;= 1000</code></li>
<li><code>1 &lt;= A[i].length &lt;= 20</code></li>
<li>All <code>A[i]</code> have the same length.</li>
<li>All <code>A[i]</code> consist of only lowercase letters.</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: Signature</strong></h1>
<p>All Special-Equivalent Strings should have the same signature if we sort all the odd-index characters and all the even-index characters.</p>
<p>E.g. [&#8220;abcd&#8221;,&#8221;cdab&#8221;,&#8221;adcb&#8221;,&#8221;cbad&#8221;] are all in the same graph.</p>
<p>&#8220;abcd&#8221;: odd &#8220;ac&#8221;, even: &#8220;bd&#8221;<br />
&#8220;cdab&#8221;: odd &#8220;ac&#8221;, even: &#8220;bd&#8221;<br />
&#8220;adcb&#8221;: odd &#8220;ac&#8221;, even: &#8220;bd&#8221;<br />
&#8220;cbad&#8221;: odd &#8220;ac&#8221;, even: &#8220;bd&#8221;</p>
<p>We can concatenate the odd and even strings to create a hashable signature.</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  int numSpecialEquivGroups(vector&lt;string&gt;&amp; A) {
    unordered_set&lt;string&gt; s;
    for (const string&amp; a : A) {
      string odd;
      string even;
      for (int i = 0; i &lt; a.size(); ++i) {
        if (i % 2)
          odd += a[i];
        else
          even += a[i];
      }
      sort(begin(odd), end(odd));
      sort(begin(even), end(even));
      s.insert(odd + even);
    }
    return s.size();
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 16 ms
class Solution {
  public int numSpecialEquivGroups(String[] A) {
    Set&lt;String&gt; groups = new HashSet&lt;&gt;();
    for (String a: A) {
      char[] odd = new char[(a.length() + 1) / 2];
      char[] even = new char[(a.length() + 1) / 2];
      for (int i = 0; i &lt; a.length(); ++i) {
        if (i % 2 == 0)
          even[i / 2] = a.charAt(i);
        else
          odd[i / 2] = a.charAt(i);          
      }
      Arrays.sort(odd);
      Arrays.sort(even);
      String s = new String(odd) + new String(even);
      groups.add(s);
    }
    return groups.size();
  }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 56 ms
"""
class Solution:
  def numSpecialEquivGroups(self, A):
    s = set()
    for a in A:
      odd = ""
      even = ""
      for i, c in enumerate(a):
        if i % 2 == 0:
          odd += c
        else:
          even += c      
      s.add(''.join(sorted(odd) + sorted(even)))
    return len(s)</pre><p></div><h2 class="tabtitle">Python (1-linear)</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 56 ms
"""
class Solution:
  def numSpecialEquivGroups(self, A):
    return len(set([''.join(sorted(a[0::2]) + sorted(a[1::2])) for a in A]))</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-893-groups-of-special-equivalent-strings/">花花酱 LeetCode 893. Groups of Special-Equivalent Strings</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-893-groups-of-special-equivalent-strings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
