⭐ C ++ | 0 ms Beats 100% | DSU
\n\n# Approach\n Describe your approach to solving the problem. \nWe Use DSU for this Problem. Pretty Much the Basic implementation without any complications. J
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe Use DSU for this Problem. Pretty Much the Basic implementation without any complications. Just whenver two characters are to be set equivalent , set the parent of bigger parent of the characters to the smaller parent of the character. \nSee the code for more clarity.\n\n# Complexity\n- Time complexity: O(logn)\n\n- Space complexity: O(n)\n\n\n# Code\n```\nclass DSU\n{\npublic:\n\tint parent[26];\n\tDSU()\n\t{\n\t\tfor (int i = 0; i < 26; ++i)\n\t\t{\n\t\t\tparent[i] = i; // setting the parent of \'a\' to \'a\'\n\t\t}\n\t}\n\n\tint find(int n)\n\t{\n\t\tif (parent[n] == n)\n\t\t{\n\t\t\treturn n;\n\t\t}\n       \n\t\treturn parent[n] = find(parent[n]);\n\t}\n\n\tvoid unite(int x, int y)\n\t{\n        \n        int par_x = find(x);\n        int par_y = find(y);\n        \n        if(par_x == par_y){\n            return;\n        }\n\n\t\tif (par_x < par_y)\n\t\t{\n\t\t\tparent[par_y] = par_x;\n          \n\t\t}else{\n            parent[par_x] = par_y;\n\t    }\n    }\n\t\t\n};\nclass Solution {\npublic:\n   \n    string smallestEquivalentString(string s1, string s2, string baseStr) {\n\n        int n = s1.size();\n        DSU wed = DSU(); \n        for(int i=0;i<n;++i)\n        {\n            wed.unite(s1[i]-\'a\',s2[i]-\'a\');\n            \n        }\n        string res = "";\n\n        for(char p:baseStr)\n        {\n            res += char (wed.find(p-\'a\') + \'a\');\n        }\n\n        return res;\n        \n    }\n};\n```
['C++']