<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p>

<p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p>

<p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p>

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

<pre>
<strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]]
<strong>Output:</strong> &quot;bacd&quot;
<strong>Explaination:</strong> 
Swap s[0] and s[3], s = &quot;bcad&quot;
Swap s[1] and s[2], s = &quot;bacd&quot;
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]]
<strong>Output:</strong> &quot;abcd&quot;
<strong>Explaination: </strong>
Swap s[0] and s[3], s = &quot;bcad&quot;
Swap s[0] and s[2], s = &quot;acbd&quot;
Swap s[1] and s[2], s = &quot;abcd&quot;</pre>

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

<pre>
<strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]]
<strong>Output:</strong> &quot;abc&quot;
<strong>Explaination: </strong>
Swap s[0] and s[1], s = &quot;bca&quot;
Swap s[1] and s[2], s = &quot;bac&quot;
Swap s[0] and s[1], s = &quot;abc&quot;
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
	<li><code>0 &lt;= pairs.length &lt;= 10^5</code></li>
	<li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li>
	<li><code>s</code>&nbsp;only contains lower case English letters.</li>
</ul>

class UF:
    def __init__(self, n):
        self.parents = list(range(n))
        self.ranks = [1] * n
    def union(self, a, b):
        p1, p2 = self.find(a), self.find(b)
        if p1 != p2:
            if self.ranks[p1] > self.ranks[p2]:
                self.parents[p2] = p1
                self.ranks[p1] += self.ranks[p2]
                return p1
            else:
                self.parents[p1] = p2
                self.ranks[p2] += self.ranks[p1]
                return p2
        else:
            return p1

    def find(self, a):
        if a != self.parents[a]:
            self.parents[a] = self.find(self.parents[a])
        return self.parents[a]

class Solution:
    def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
        if len(s) == 1 or not pairs:
            return s
        n = len(s)
        uf = UF(n)
        sets = {}
        for edge in pairs:
            p1 = uf.find(edge[0])
            p2 = uf.find(edge[1])
            if p1 != p2:
                newp = uf.union(p1, p2)
                if newp == p1:
                    if p2 in sets:
                        sets.setdefault(newp, [newp]).extend(sets[p2])
                        sets.pop(p2)
                    else:
                        sets.setdefault(newp, [newp]).append(p2)

                else:
                    if p1 in sets:
                        sets.setdefault(newp, [newp]).extend(sets[p1])
                        sets.pop(p1)
                    else:
                        sets.setdefault(newp, [newp]).append(p1)
        ret = list(s)
        for l in sets.values():
            cands = [s[idx] for idx in l]
            for idx, c in zip(sorted(l), sorted(cands)):
                ret[idx] = c
        return "".join(ret)
                
            

491