图论29(Leetcode1061按字典序排列最小的等效字符串)

代码:

class Solution {
    public String smallestEquivalentString(String s1, String s2, String baseStr) {
        Map charToIndex = new HashMap<>();
        Map indexToChar = new HashMap<>();
        int charsCount = 0;
        for(char c:s1.toCharArray()){
            if(!charToIndex.containsKey(c)){
                charToIndex.put(c,charsCount);
                indexToChar.put(charsCount++,c);
            }
        }
        for(char c:s2.toCharArray()){
            if(!charToIndex.containsKey(c)){
                charToIndex.put(c,charsCount);
                indexToChar.put(charsCount++,c);
            }
        }
        for(char c:baseStr.toCharArray()){
            if(!charToIndex.containsKey(c)){
                charToIndex.put(c,charsCount);
                indexToChar.put(charsCount++,c);
            }
        }
        
        UnionFind uf = new UnionFind(charsCount);
        for(int i=0;i

你可能感兴趣的:(图论,算法,数据结构)