✅✅100% faster & Easy to understand || Union Find || 0ms runtime✅✅
Approach\n Disjoint set Union(DSU)/Union Find\n\n# Complexity\n- Time complexity:\nApprox. O(n log(n)) \n\n- Space complexity:\nO(26)\n\n# Code\n\nclass Solutio
# Approach\n Disjoint set Union(DSU)/Union Find\n\n# Complexity\n- Time complexity:\nApprox. O(n log(n)) \n\n- Space complexity:\nO(26)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int>parent;\n    int find(int x){  //find function returns the parent of set\n      if(parent[x]==x){\n          return x;\n      }\n      return parent[x]=find(parent[x]);\n    }\n    void union_set(int x,int y){ // make union of the set a & b if they belongs to different set\n        x=find(x);\n        y=find(y);\n        if(x==y){                //if they belong to same set simply return.\n            return;\n        }\n        parent[max(x,y)]=min(x,y); //making lexicographically smallest char as parent of the union set\n    }\n    string smallestEquivalentString(string s1, string s2, string baseStr) {\n        parent.resize(26);\n        for(int i=0;i<26;i++){\n            parent[i]=i;\n        }\n        for(int i=0;i<s1.size();i++){ // s1[1] and s2[i] are similarto each other hence they are put in same set.\n          int a=s1[i]-\'a\';\n          int b=s2[i]-\'a\';\n          union_set(a,b);    // make union of the set a & b if they belongs to different set\n        }\n        string ans;\n        for(auto x:baseStr){\n            char k=find(x-\'a\')+\'a\';    // here find function returns the lexographically smallest char from the set\n            ans+=k;\n        }\n        return ans;\n    }\n};\n```\n\u2705\u2705PLease upvote if you find it useful.\n\n![Screenshot 2023-01-14 154613.png](https://assets.leetcode.com/users/images/6ce4e72f-2605-483f-94e5-8fc8fef881bd_1673692098.7948232.png)\n
['String', 'Union Find', 'C++']