✅Complete explanation of each step || Daily LeetCode solution || C++
DO UPVOTE IF YOU LIKE\n# Intuition \n1. map the character s1[i] with s2[i] and s2[i] with s1[i] \n\nmap<char,set<char>>mp;\n     for(int i=0;i<s1.size();i++)\n 
## **DO UPVOTE IF YOU LIKE**\n# Intuition \n1. map the character s1[i] with s2[i] and s2[i] with s1[i] \n```\nmap<char,set<char>>mp;\n     for(int i=0;i<s1.size();i++)\n     {\n       mp[s1[i]].insert(s2[i]);\n       mp[s2[i]].insert(s1[i]);\n     } \n\n```\n2. now we will iterate mapping first time\n   here we will be putting all the elements from set of every   \n  element in set of characters mapped with current character that we are iterating via map\n\n```\n   for(auto it=mp.begin();it!=mp.end();it++) //its not commented \n     {\n         for(auto itin:it->second)\n         {\n           for(auto itt:mp[itin])\n               mp[it->first].insert(itt);\n         }\n     }\n```\n3. now we will repeat above same step but just in reverse order so that if any character gets associated afterwards then also we can consider it\n \n ```\nfor(auto it=mp.rbegin();it!=mp.rend();it++) //it is also not commented\n     {\n         for(auto itin:it->second)\n         {\n           for(auto itt:mp[itin])\n               mp[it->first].insert(itt);\n         }\n     }\n```\ne.g for the testcase -\n![image.png](https://assets.leetcode.com/users/images/4c2043dc-aa44-4385-9af7-9d1656817301_1673685491.9578443.png)\n\n     \nbefore iterating reverse\n    \n![image.png](https://assets.leetcode.com/users/images/b58291c4-e251-4f0c-b307-96de37320d37_1673685397.652144.png)\nAfter iterating reverse\n     \n![image.png](https://assets.leetcode.com/users/images/fdbf2b0b-466d-4c54-bef0-3c4188f91264_1673685255.0946136.png)\n      \n  \n4. then just replace the character in basestring by smallest character in its mapping ,and if no mapping exists then keep it as it is\n\n```\nfor(int i=0;i<baseStr.size();i++)\n     {\n         char mini=\'z\';\n         for(int j=0;j<mp[baseStr[i]].size();j++)\n         {\n         for(auto it:mp[baseStr[i]])\n              mini=min(mini,it);\n         }\n         baseStr[i]=min(mini,baseStr[i]);\n     }\n```\n# Code\n```\nclass Solution {\npublic:\n    string smallestEquivalentString(string s1, string s2, string baseStr) {\n     map<char,set<char>>mp;\n     for(int i=0;i<s1.size();i++)\n     {\n       mp[s1[i]].insert(s2[i]);\n       mp[s2[i]].insert(s1[i]);\n     } \n     for(auto it=mp.begin();it!=mp.end();it++)  //from front\n     {\n         for(auto itin:it->second)\n         {\n           for(auto itt:mp[itin])\n               mp[it->first].insert(itt);\n         }\n     }\n      for(auto it=mp.rbegin();it!=mp.rend();it++) //from back\n     {\n         for(auto itin:it->second)\n         {\n           for(auto itt:mp[itin])\n               mp[it->first].insert(itt);\n         }\n     }\n\n     for(int i=0;i<baseStr.size();i++)\n     {\n         char mini=\'z\';\n         for(int j=0;j<mp[baseStr[i]].size();j++)\n         {\n         for(auto it:mp[baseStr[i]])\n              mini=min(mini,it);\n         }\n         baseStr[i]=min(mini,baseStr[i]);\n     }\n     return baseStr;\n    }\n};\n```
['String', 'Ordered Map', 'Ordered Set', 'C++']