Easy | Python Solution | Hashmaps | Arrays | Sets
Code\n\nclass Solution:\n    def smallestEquivalentString(self, s1: str, s2: str, baseStr: str) -> str:\n        hashmap = defaultdict(list)\n\n        for i in
# Code\n```\nclass Solution:\n    def smallestEquivalentString(self, s1: str, s2: str, baseStr: str) -> str:\n        hashmap = defaultdict(list)\n\n        for i in range(len(s1)):\n            hashmap[s1[i]].append(s2[i])\n            hashmap[s2[i]].append(s1[i])\n\n        self.visited = set()\n        self.res = defaultdict(list)\n        self.temp = []\n\n        def dfs(node):\n            if node not in self.visited:\n                self.visited.add(node)\n                self.temp.append(node)\n                for i in hashmap[node]:\n                    dfs(i)\n\n        for key in hashmap:\n            dfs(key)\n            self.temp.sort()\n\n            for i in self.temp:\n                self.res[i] += self.temp\n            self.temp = []\n\n        res = ""\n        \n        for i in baseStr:\n            if len(self.res[i]) != 0:\n                res += self.res[i][0]\n            else:\n                res += i\n\n        return res\n\n\n```\nDo upvote if you like the solution :)\n
['Python3']