<p>Two strings, <code>X</code> and <code>Y</code>, are considered similar if either they are identical or we can make them equivalent by swapping at most two letters (in distinct positions) within the string <code>X</code>.</p>

<p>For example, <code>&quot;tars&quot;</code>&nbsp;and <code>&quot;rats&quot;</code>&nbsp;are similar (swapping at positions <code>0</code> and <code>2</code>), and <code>&quot;rats&quot;</code> and <code>&quot;arts&quot;</code> are similar, but <code>&quot;star&quot;</code> is not similar to <code>&quot;tars&quot;</code>, <code>&quot;rats&quot;</code>, or <code>&quot;arts&quot;</code>.</p>

<p>Together, these form two connected groups by similarity: <code>{&quot;tars&quot;, &quot;rats&quot;, &quot;arts&quot;}</code> and <code>{&quot;star&quot;}</code>.&nbsp; Notice that <code>&quot;tars&quot;</code> and <code>&quot;arts&quot;</code> are in the same group even though they are not similar.&nbsp; Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.</p>

<p>We are given a list <code>strs</code> of strings where every string in <code>strs</code> is an anagram of every other string in <code>strs</code>. How many groups are there?</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;tars&quot;,&quot;rats&quot;,&quot;arts&quot;,&quot;star&quot;]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;omv&quot;,&quot;ovm&quot;]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= strs.length &lt;= 300</code></li>
	<li><code>1 &lt;= strs[i].length &lt;= 300</code></li>
	<li><code>strs[i]</code> consists of lowercase letters only.</li>
	<li>All words in <code>strs</code> have the same length and are anagrams of each other.</li>
</ul>

class UnionFind
{
public:
    string find(const string &s)
    {
        if(parents.find(s) == parents.end())
        {
            parents[s] = s;
            ranks[s] = 1;
        }

        if(parents[s] != s)
        {
            parents[s] = find(parents[s]);
        }

        return parents[s];
    }

    void unite(const std::string &u, const std::string &v)
    {
        string rootU = find(u);
        string rootV = find(v);

        if(rootU == rootV)
        {
            return;
        }

        if(ranks[rootU] > ranks[rootV])
        {
            parents[rootV] = rootU;
        } else if(ranks[rootU] < ranks[rootV])
        {
            parents[rootU] = rootV;
        } else
        {
            parents[rootV] = rootU;
            ranks[rootU]++;
        }
    }


private:
    unordered_map<string, string> parents;
    unordered_map<string, int> ranks;
};

class Solution {
public:

    int n;

    bool isSame(const std::string &left, const std::string &right)
    {
        int s1 = left.size();
        int s2 = right.size();

        if(s1 != s2)
        {
            return false;
        }

        vector<int> diff1;
        vector<int> diff2;
        int count = 0;
        for(int i = 0; i < s1; ++i )
        {
            if(left[i] != right[i])
            {
                count++;
                diff1.push_back(left[i]);
                diff2.push_back(right[i]);
            }

            if(count > 2)
            {
                return false;
            }
        }

        std::sort(diff1.begin(), diff1.end());
        std::sort(diff2.begin(), diff2.end());

        if(diff1 != diff2)
        {
            return false;
        }

        return true;
    }

    int numSimilarGroups(vector<string>& strs) {

        n = strs.size();

        UnionFind uf;
        for(int i = 0; i < strs.size(); ++i)
        {
            string u = strs[i];
            for(int j = i + 1; j < strs.size(); ++j)
            {
                string v = strs[j];
                bool same = isSame(u, v);
                if(same)
                {
                    uf.unite(u,v);
                }
            }
        }

        std::unordered_set<string> sets;
        for(int i = 0; i < strs.size(); ++i)
        {
            string root = uf.find(strs[i]);
            sets.insert(root);
        }

        return sets.size();
    }
};
146706