1061. Lexicographically Smallest Equivalent String

Difficulty:
Related Topics:
Similar Questions:

    Problem

    You are given two strings of the same length s1 and s2 and a string baseStr.

    We say s1[i] and s2[i] are equivalent characters.

    Equivalent characters follow the usual rules of any equivalence relation:

    For example, given the equivalency information from s1 = "abc" and s2 = "cde", "acd" and "aab" are equivalent strings of baseStr = "eed", and "aab" is the lexicographically smallest equivalent string of baseStr.

    Return **the lexicographically smallest equivalent string of *baseStr* by using the equivalency information from s1 and **s2.

      Example 1:

    Input: s1 = "parker", s2 = "morris", baseStr = "parser"
    Output: "makkek"
    Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
    The characters in each group are equivalent and sorted in lexicographical order.
    So the answer is "makkek".
    

    Example 2:

    Input: s1 = "hello", s2 = "world", baseStr = "hold"
    Output: "hdld"
    Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
    So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
    

    Example 3:

    Input: s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
    Output: "aauaaaaada"
    Explanation: We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
    

      Constraints:

    Solution (Java)

    class Solution {
        class DSU{
            ArrayList<Integer> parent = new ArrayList<Integer>();
            ArrayList<Integer> rank = new ArrayList<>();
            public DSU()
            {
                for(int i = 0;i<26;i++)
                {
                    parent.add(i);
                    rank.add(i);
                }
            }
            public int getParent(int node)
            {
                if(node == parent.get(node))
                    return node;
                int gParent = getParent(parent.get(node));
                parent.set(node, gParent);
                return parent.get(node);
            }
            public void union(int nodeU, int nodeV)
            {
                int parentU = getParent(nodeU);
                int parentV = getParent(nodeV);
                if(parentU == parentV)
                    return;
                if(rank.get(parentU)< rank.get(parentV))
                {
                    parent.set(parentV, parentU);
                }
                else{
                    parent.set(parentU, parentV);
                }
            }
        }
        public String smallestEquivalentString(String s1, String s2, String baseStr) {
            DSU dsu = new DSU();
            for(int i = 0;i<s1.length();i++)
            {
                dsu.union(s1.charAt(i)-'a', s2.charAt(i)-'a');
            }
            StringBuffer result = new StringBuffer();
            for(int i= 0;i<baseStr.length();i++)
            {
                char ch = (char)((dsu.getParent(baseStr.charAt(i)-'a')) + 'a');
                result.append(ch);
            }
            return result.toString();
    
        }
    }
    

    Explain:

    nope.

    Complexity: