All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1001_1100.s1061_lexicographically_smallest_equivalent_string.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g1001_1100.s1061_lexicographically_smallest_equivalent_string;

// #Medium #String #Union_Find #2023_06_09_Time_2_ms_(93.75%)_Space_41.7_MB_(41.30%)

/**
 * 1061 - Lexicographically Smallest Equivalent String\.
 *
 * Medium
 *
 * 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.
 *
 * *   For example, if `s1 = "abc"` and `s2 = "cde"`, then we have `'a' == 'c'`, `'b' == 'd'`, and `'c' == 'e'`.
 *
 * Equivalent characters follow the usual rules of any equivalence relation:
 *
 * *   **Reflexivity:** `'a' == 'a'`.
 * *   **Symmetry:** `'a' == 'b'` implies `'b' == 'a'`.
 * *   **Transitivity:** `'a' == 'b'` and `'b' == 'c'` implies `'a' == 'c'`.
 *
 * 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:**
 *
 * *   `1 <= s1.length, s2.length, baseStr <= 1000`
 * *   `s1.length == s2.length`
 * *   `s1`, `s2`, and `baseStr` consist of lowercase English letters.
**/
public class Solution {
    private int[] parent;

    public String smallestEquivalentString(String s1, String s2, String baseStr) {
        parent = new int[26];
        int n = s1.length();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 26; i++) {
            parent[i] = i;
        }
        for (int i = 0; i < n; i++) {
            union(s1.charAt(i) - 'a', s2.charAt(i) - 'a');
        }
        char base = 'a';
        for (char element : baseStr.toCharArray()) {
            result.append(Character.toString(base + find(element - 'a')));
        }
        return result.toString();
    }

    private void union(int a, int b) {
        int parentA = find(a);
        int parentB = find(b);
        if (parentA != parentB) {
            if (parentA < parentB) {
                parent[parentB] = parentA;
            } else {
                parent[parentA] = parentB;
            }
        }
    }

    private int find(int x) {
        while (parent[x] != x) {
            x = parent[x];
        }
        return x;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy