Java Easy BFS solution with explanation
\n# Approach\n Describe your approach to solving the problem. \nWe create a undirected graph where the equivelancy are the edge ie; (\'a\'==\'c\') means there i
\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe create a undirected graph where the equivelancy are the edge ie; (\'a\'==\'c\') means there is an edge from a to c.\n(The graph here is not a connected graph so we have to do BFS search node by node.)\nWe then do a BFS search on the graph node by node and find the smallest character and save it as equivelant for all the characters that were involved on the search.\nwe convert each character in the baseStr with the equivelant character.\n\n\n# Code\n```\nclass Solution {\n    public String smallestEquivalentString(String s1, String s2, String baseStr) {\n        \n        char[] l = new char[26];\n        for(char i=0;i<l.length;i++)\n        {\n          \n          char c =(char)(\'a\'+i);\n          l[i]=c;\n\n        }\n        HashMap<Character,List<Character>> m = new HashMap<Character,List<Character>>();\n        for(int i=0;i<s1.length();i++)\n        {\n            char c1 = s1.charAt(i);\n            char c2 = s2.charAt(i);\n            if(c1==c2)\n                continue;\n           List<Character> list =m.getOrDefault(c1,new ArrayList());\n           if(!list.contains(c2))\n                    list.add(c2);\n            m.put(c1,list);\n           list =m.getOrDefault(c2,new ArrayList());\n           if(!list.contains(c1))\n                list.add(c1);\n            m.put(c2,list);    \n\n\n        }\n\n\n         List<Character> memory =new ArrayList();\n        for(char i=0;i<l.length;i++)\n        {\n         \n           char c =l[i];\n           if(memory.contains(c))\n            continue;\n           List<Character> list = m.getOrDefault(c,new ArrayList());\n           List<Character> itr =new ArrayList();\n           List<Character> visited =new ArrayList();\n           itr.addAll(list);\n           while(!itr.isEmpty())\n           {\n               char r = itr.remove(0);\n               if(r<c)\n                    c=r;\n               list = m.get(r);\n               for(char j:list)\n                if(!visited.contains(j))\n                    {\n                        itr.add(j);\n                        visited.add(j);\n                    }\n           }\n\n           for(char j:visited)\n             l[j-\'a\']=c;\n           memory.addAll(visited);\n\n        }\n        String retStr="";\n            \n        for(int i=0;i<baseStr.length();i++)\n        {\n            char c=baseStr.charAt(i); \n            char smallest=l[c-\'a\'];\n           retStr=retStr+smallest +"";                \n        }\n\n        return retStr;\n    }\n}\n```
['Java']