<p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p>

<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p>

<ul>
	<li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>&#39;a&#39; == &#39;c&#39;</code>, <code>&#39;b&#39; == &#39;d&#39;</code>, and <code>&#39;c&#39; == &#39;e&#39;</code>.</li>
</ul>

<p>Equivalent characters follow the usual rules of any equivalence relation:</p>

<ul>
	<li><strong>Reflexivity:</strong> <code>&#39;a&#39; == &#39;a&#39;</code>.</li>
	<li><strong>Symmetry:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> implies <code>&#39;b&#39; == &#39;a&#39;</code>.</li>
	<li><strong>Transitivity:</strong> <code>&#39;a&#39; == &#39;b&#39;</code> and <code>&#39;b&#39; == &#39;c&#39;</code> implies <code>&#39;a&#39; == &#39;c&#39;</code>.</li>
</ul>

<p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p>

<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p>

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

<pre>
<strong>Input:</strong> s1 = &quot;parker&quot;, s2 = &quot;morris&quot;, baseStr = &quot;parser&quot;
<strong>Output:</strong> &quot;makkek&quot;
<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is &quot;makkek&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;hello&quot;, s2 = &quot;world&quot;, baseStr = &quot;hold&quot;
<strong>Output:</strong> &quot;hdld&quot;
<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter &#39;o&#39; in baseStr is changed to &#39;d&#39;, the answer is &quot;hdld&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;leetcode&quot;, s2 = &quot;programs&quot;, baseStr = &quot;sourcecode&quot;
<strong>Output:</strong> &quot;aauaaaaada&quot;
<strong>Explanation:</strong> We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except &#39;u&#39; and &#39;d&#39; are transformed to &#39;a&#39;, the answer is &quot;aauaaaaada&quot;.
</pre>

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

<ul>
	<li><code>1 &lt;= s1.length, s2.length, baseStr &lt;= 1000</code></li>
	<li><code>s1.length == s2.length</code></li>
	<li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li>
</ul>

class Solution {
public:
// Node struct to represent a character and its properties in the Disjoint-Set
struct Node{
    int id =-1; // id of the character, initialize it to -1 to represent uninitialized state
    char label=' '; // label of the character, initialize it to ' ' to represent uninitialized state
    Node* parent=this; // pointer to the parent node, initialize it to this to represent the root node
    int size=1; // size of the set, initialize it to 1 as it is the root node
    bool visted=0; // boolean to check if the node is visited, not used in this code
    vector<Node*> neighbours={}; // neighbours of the node, not used in this code
    Node(int _id, char _label):id(_id), label(_label){} // constructor to initialize the id and label of the node
};

// function to find the parent of a node in the Disjoint-Set
Node* getParentDSU(Node* node){
    if(node==node->parent) return node; // if the node is the root node, return it
    return node->parent=getParentDSU(node->parent); // else recursively find the parent and use path compression to update the parent
}

// function to join two nodes in the Disjoint-Set
void joinDSU(Node* src,Node* tar){
    src=getParentDSU(src); // find the parent of the source node
    tar=getParentDSU(tar); // find the parent of the target node
    if(src!=tar){ // if the two parents are different
        if(tar->label<src->label) swap(src,tar); // ensure the label of the parent is lexicographically smaller
        tar->parent=src; // set the parent of target to source
        src->size+=tar->size; // increment the size of the set
    }
}

string smallestEquivalentString(string s1, string s2, string baseStr) {
    // Create an array of nodes representing each character
    vector<Node*> nodes(26);
    for(int i=0;i<26;i++){
        nodes[i] =  new Node(i+1,'a'+i); // initialize the nodes with id and label
    }

    // For each pair of characters in s1 and s2, join their parents in the Disjoint-Set
    for(int i=0;i<s1.size();i++){
            joinDSU(nodes[s1[i]-'a'],nodes[s2[i]-'a']);
    }

    // For each character in baseStr, replace it with its equivalent parent in the Disjoint-Set
    for(int i=0;i<baseStr.size();i++){
        if(getParentDSU(nodes[baseStr[i]-'a'])->label<baseStr[i]){
            baseStr[i]=getParentDSU(nodes[baseStr[i]-'a'])->label;
        }
   
}

    return baseStr;
}
};
8500