Union Find Class with Path Compression and Rank
Code\n\n/**\n * @param {string} s1\n * @param {string} s2\n * @param {string} baseStr\n * @return {string}\n */\nvar smallestEquivalentString = function(s1, s2,
# Code\n```\n/**\n * @param {string} s1\n * @param {string} s2\n * @param {string} baseStr\n * @return {string}\n */\nvar smallestEquivalentString = function(s1, s2, baseStr) {\n    const uf = new UnionFind // instantiate new union find class\n    let a = \'\' // variable \'a\' to represent the answer\n\n    for (let i = 0; i < s1.length; i++) {\n        uf.union(s1[i], s2[i]) // call the union method to join both s1 and s2 chars\n    }\n\n    for (const c of baseStr) // variable \'c\' for character\n        a += uf.find(c) // returns us the best leader\n\n    return a\n}\n\nclass UnionFind {\n    constructor() {\n        this.uf = {} // we use an object as a hashmap \n    }\n\n    find(c) {\n        if (!this.uf[c]) this.uf[c] = c\n        while (this.uf[c] !== c) { // compress the path\n            this.uf[c] = this.uf[this.uf[c]] // set uf[c] to top level parent\n            c = this.uf[c]\n        }\n        return c\n    }\n    \n    union(c1, c2) { // character1 and character2\n        c1 = this.find(c1)\n        c2 = this.find(c2)\n        if (c1 > c2) { // optimize via ranking\n            let t = c2\n            c2 = c1\n            c1 = t\n        }\n        this.uf[c2] = c1\n    }\n}\n```
['JavaScript']