JavaScript - Union Find
Intuition\n Describe your first thoughts on how to solve this problem. \nRepresent the problem as a graph problem. Each group of characters represents a connect
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRepresent the problem as a graph problem. Each group of characters represents a connected component. To solve this use Union Find to group the characters and chose the lexicographically smallest member of each set to be the set\'s representative.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nImplement Union Find. Unite all `s1[i]` and `s2[i]` for all `1 <= i <n `. Then iterate through the characters of  `baseStr` and replace each with the representative of its the group (the union find implementation should chose the smallest character as representative).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n log26 + m)$$ \nWhere `n` is the length of `s1` and `s2`. `Log26` comes from the complexity of union find with path compression and there\'s only 26 english chars. `m` is the length of `baseStr`\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(26 + m)$$ \nfrom the `link` map and building the answer string\n\n# Code\n```\nconst smallestEquivalentString = (s1, s2, baseStr) => {\n    const link = {};\n    const find = (a)=>{\n        if(link[a] == null) link[a] = a;\n        while(link[a] != a){\n            link[a] = link[link[a]];\n            a = link[a];\n        }\n        return a;\n    }\n    const union = (a, b)=>{\n        a = find(a);\n        b = find(b);\n        if(a > b){\n            let temp = b;\n            b = a;\n            a = temp;\n        }\n        link[b] = a;\n    }\n    for(let i = 0; i < s1.length; i++){\n        union(s1[i], s2[i]);\n    }\n\n    let ans = "";\n    for(const a of baseStr){\n        ans+=find(a);\n    }\n    return ans;\n};\n```
['Union Find', 'JavaScript']