✅Easy solution || ✅Short & Simple || ✅Best Method || ✅Easy-To-Understand
Intuition\n\nPlease UPVOTE if you LIKE! \uD83D\uDE01\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:\n Add y
# Intuition\n\nPlease UPVOTE if you LIKE! \uD83D\uDE01\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n      char dfs(unordered_map<char, vector<char>>& mapping, char input, unordered_set<char>& visited) {\n        if (visited.count(input) > 0) {\n            return input;\n        }\n        visited.insert(input);\n        char ret = input;\n        for (const auto ch : mapping[input]) {\n            ret = min(ret, dfs(mapping, ch, visited));\n        }\n        return ret;\n    }\npublic:\n    string smallestEquivalentString(string A, string B, string S) {\n        unordered_map<char, vector<char>> mapping;\n        for (int i = 0; i < A.size(); ++i) {\n            mapping[A[i]].push_back(B[i]);\n            mapping[B[i]].push_back(A[i]);\n        }\n        string ret;\n        unordered_map<char, char> memo;\n        for (const auto s : S) {\n            if (memo.count(s) > 0) {\n                ret += memo[s];\n                continue;\n            }\n            unordered_set<char> visited;\n            const auto min_char = dfs(mapping, s, visited);\n            for (const auto ch : visited) {\n                memo[ch] = min_char;\n            }\n            ret += min_char;\n        }\n        return ret;\n    }\n};\n```
['C++']