C++ || Union-find by character size || Easy Explaination
Approach\n Describe your approach to solving the problem. \nUnion-Find Concept of graph. See the commented code for better explanation.\n\n# Code\n\n\n\nclass S
# Approach\n<!-- Describe your approach to solving the problem. -->\nUnion-Find Concept of graph. See the commented code for better explanation.\n\n# Code\n```\n\n\nclass Solution {\npublic:\n    vector<int> parent;\n    // return the ultimate parent (representative) of the component containing x as one of its element.\n    int find(int x){\n        if(parent[x]==x)    return x;\n        \n        return parent[x] = find(parent[x]);\n    }\n    // Perform union if a and b aren\'t in the same component.\n    void _union(int a , int b){\n        int x = find(a);\n        int y = find(b);\n        // return if a and b are in the same component\n        if(x==y)    return;             \n        // Make the smaller character parent of the other one as we want the lexicograohically smallest string.\n        if(x > y)   parent[x] = y;\n        else        parent[y] = x;\n    }\n    string smallestEquivalentString(string s1, string s2, string baseStr) {\n        parent.resize(26);\n        // Make each character parent of itself.\n        for(int i=0 ; i<26 ; i++){\n            parent[i]=i;\n        }\n        // Perform union merge for all the pairs of characters from both the strings s1 and s2.\n        for(int i=0 ; i<s1.size() ; i++){\n            _union(s1[i]-\'a\' , s2[i]-\'a\');\n        }\n        // Create the answer string with final parent vector.\n        string res="";\n        for(char ch : baseStr){\n            res += (char)(find(ch-\'a\')+\'a\');\n        }\n        return res;\n    }\n};\n```
['String', 'Depth-First Search', 'Union Find', 'Graph', 'C++']