C++ || Easily Explained With Comments || DSU
\nclass Solution {\npublic:\n    \n    /* Simple piece of code used multiple times for union find \n    operations along with path compression and union by rank
```\nclass Solution {\npublic:\n    \n    /* Simple piece of code used multiple times for union find \n    operations along with path compression and union by rank \n    optimizations. If you wish refer to CP-ALGORITHM documentation for\n    more clearity on DSU . */\n    \n    int Parent[26];\n    int Rank[26];\n    \n    // Path Compression Optimization\n    \n    int find(int x){\n        if(Parent[x] == x) return x;\n        return Parent[x] = find(Parent[x]);\n    }\n    \n    // Union by Rank Optimization\n    \n    void Union(int x,int y){\n        int lx = find(x);\n        int ly = find(y);\n        if(lx != ly){\n            if(Rank[lx] < Rank[ly]){\n                Parent[lx] = ly;\n            }\n            else{\n                Parent[ly] = lx;\n                if(Rank[lx] == Rank[ly]) Rank[lx]++;\n            }\n        }\n    }\n    \n    string smallestEquivalentString(string s1, string s2, string baseStr) {\n        int n = s1.length();\n        for(int i=0;i<26;i++){\n            Parent[i] = i;\n            Rank[i] = 1;\n        }\n        \n        // Grouping of all characters to access their leader \n        \n        for(int i=0;i<n;i++){\n            Union(s1[i]-\'a\',s2[i]-\'a\');\n        }\n        \n        vector<char>minElement(26);\n        \n        /* First initialize all the characters with their current \n        value , e.g \'a\' -> \'a\' , \'c\' -> \'c\' and so on. */\n        \n        for(int i=0;i<26;i++){\n            minElement[i] = (char)(i+\'a\');\n        }\n        \n        /* first figure out the current element belongs to which group\n        and then mark its leader\'s value with the minimum value among\n        all the characters of the same group-members. */\n        \n        for(int i=0;i<26;i++){\n            int par = find(i);\n            minElement[par] = min(minElement[par],(char)(i+\'a\'));\n        }\n        \n        /* Now move on to the baseStr , and for each character , and\n        figure out the group leader of the group , in which the \n        current character belongs to. */\n        \n        /* Then just put the minElement[group_leader] for each and \n        every character , as they are already precomputed by us \n        earlier. */\n        \n        for(int i=0;i<baseStr.length();i++){\n            int par = find(baseStr[i]-\'a\');\n            baseStr[i] = minElement[par];\n        }\n        \n        return baseStr;\n        \n    }\n};\n```
[]