Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
1
Indexable
Never
class Solution {

    public int getGreaterChar(Character c1, Character c2){
        if(c1.compareTo(c2)>0){
            return c1;
        }
        return c2;
    }
    public String smallestEquivalentString(String s1, String s2, String baseStr) {
        HashMap<Character, Character> hmap = new HashMap<Character, Character>();

        int len = s1.length();
        int n = baseStr.length();

        for(int i=0; i<len; i++){
            Character c1 = s1.charAt(i);
            Character c2 = s2.charAt(i);
            if(c1!=c2){
                if(c1.compareTo(c2)>0){
                    Character c3 = c1;
                    while(hmap.containsKey(c1) && (c1!=hmap.get(c1))){
                        c3 = hmap.get(c1);
                        c1 = c3;
                    }
                    int compare = c3.compareTo(c2);
                    if(compare>0){
                        hmap.put(c1, c2);
                        hmap.put(c3, c2);
                    }
                    else if(compare<0){
                        hmap.put(c2, c3);
                    }
                }
                else if(c1.compareTo(c2)<0){
                    Character c3 = c2;
                    while(hmap.containsKey(c2) && (c2!=hmap.get(c2))){
                        c3 = hmap.get(c2);
                        c2 = c3;
                    }
                    int compare = c3.compareTo(c1);
                    if(compare>0){
                        hmap.put(c2, c1);
                        hmap.put(c3, c1);
                    }
                    else if(compare<0){
                        hmap.put(c1, c3);
                    }
                }
            }
        }
        System.out.println(hmap);
        StringBuilder answer = new StringBuilder();

        for(int i=0; i<n; i++){
            Character c1 = baseStr.charAt(i);
            while(hmap.containsKey(c1) && (c1!=hmap.get(c1))){
                Character c2 = hmap.get(c1);
                c1 = c2;
            }
            answer.append(c1);
        }

        return answer.toString();
    }
}