2 Solutions (1ms & 4ms) 🔥🔥 || With Explanation✅|| Java😊
Explaination for Approach 1 :\nUse two maps to store each letter with its group and each group with  the letters in the group, respectively. Initially, all lett
# Explaination for Approach 1 :\nUse two maps to store each letter with its group and each group with  the letters in the group, respectively. Initially, all letters have different groups, and each group has only one letter, which is the letter itself.\n\nLoop over A and B and merge each pair of letters into one group. When merging into one group, only remain the group that is lexicographically smallest. All the letters from both previous groups are mapped to the remaining group, and the remaining group is mapped to all the letters from both previous groups.\n\nAfter the mappings are done, loop over S and obtain the group for each letter and replace the letter in S. Finally, return the new string.\n# Request \uD83D\uDE0A :\nIf you find these solutions easy to understand and helpful, then . Please Upvote\uD83D\uDC4D\uD83D\uDC4D (Scroll Down for fastest [1 ms] approach).\n# Approach 1 : 4 ms Solution\n```\nclass Solution {\n    public String smallestEquivalentString(String A, String B, String S) {\n        Map<Character, Character> letterGroupMap = new HashMap<Character, Character>();\n        Map<Character, Set<Character>> groupLettersMap = new HashMap<Character, Set<Character>>();\n        for (char c = \'a\'; c <= \'z\'; c++) {\n            letterGroupMap.put(c, c);\n            Set<Character> set = new HashSet<Character>();\n            set.add(c);\n            groupLettersMap.put(c, set);\n        }\n        int length = A.length();\n        for (int i = 0; i < length; i++) {\n            char c1 = A.charAt(i), c2 = B.charAt(i);\n            char group1 = letterGroupMap.get(c1), group2 = letterGroupMap.get(c2);\n            if (group1 != group2) {\n                Set<Character> set1 = groupLettersMap.get(group1);\n                Set<Character> set2 = groupLettersMap.get(group2);\n                if (group1 < group2) {\n                    for (char c : set2)\n                        letterGroupMap.put(c, group1);\n                    set1.addAll(set2);\n                    groupLettersMap.put(group1, set1);\n                    groupLettersMap.remove(group2);\n                } else {\n                    for (char c : set1)\n                        letterGroupMap.put(c, group2);\n                    set2.addAll(set1);\n                    groupLettersMap.put(group2, set2);\n                    groupLettersMap.remove(group1);\n                }\n            }\n        }\n        char[] array = S.toCharArray();\n        int strLength = array.length;\n        for (int i = 0; i < strLength; i++) {\n            char c = array[i];\n            char group = letterGroupMap.get(c);\n            array[i] = group;\n        }\n        return new String(array);\n    }\n}\n```\n\n# Approach 2 : 1 ms Solution [Fastest]\n```\nclass Solution {\n    private int[] p;\n\n    public String smallestEquivalentString(String s1, String s2, String baseStr) {\n        p = new int[26];\n        for (int i = 0; i < 26; ++i) {\n            p[i] = i;\n        }\n        for (int i = 0; i < s1.length(); ++i) {\n            int a = s1.charAt(i) - \'a\', b = s2.charAt(i) - \'a\';\n            int pa = find(a), pb = find(b);\n            if (pa < pb) {\n                p[pb] = pa;\n            } else {\n                p[pa] = pb;\n            }\n        }\n        StringBuilder sb = new StringBuilder();\n        for (char a : baseStr.toCharArray()) {\n            char b = (char) (find(a - \'a\') + \'a\');\n            sb.append(b);\n        }\n        return sb.toString();\n    }\n\n    private int find(int x) {\n        if (p[x] != x) {\n            p[x] = find(p[x]);\n        }\n        return p[x];\n    }\n}\n```\n
['Hash Table', 'String', 'Union Find', 'Java']