1061. Lexicographically Smallest Equivalent String Medium

@problem@discussion
#String#Union Find



1/**
2 * [1061] Lexicographically Smallest Equivalent String
3 *
4 * You are given two strings of the same length s1 and s2 and a string baseStr.
5 * We say s1[i] and s2[i] are equivalent characters.
6 * 
7 * 	For example, if s1 = "abc" and s2 = "cde", then we have 'a' == 'c', 'b' == 'd', and 'c' == 'e'.
8 * 
9 * Equivalent characters follow the usual rules of any equivalence relation:
10 * 
11 * 	Reflexivity: 'a' == 'a'.
12 * 	Symmetry: 'a' == 'b' implies 'b' == 'a'.
13 * 	Transitivity: 'a' == 'b' and 'b' == 'c' implies 'a' == 'c'.
14 * 
15 * 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.
16 * Return the lexicographically smallest equivalent string of baseStr by using the equivalency information from s1 and s2.
17 *  
18 * <strong class="example">Example 1:
19 * 
20 * Input: s1 = "parker", s2 = "morris", baseStr = "parser"
21 * Output: "makkek"
22 * 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].
23 * The characters in each group are equivalent and sorted in lexicographical order.
24 * So the answer is "makkek".
25 * 
26 * <strong class="example">Example 2:
27 * 
28 * Input: s1 = "hello", s2 = "world", baseStr = "hold"
29 * Output: "hdld"
30 * Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
31 * So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
32 * 
33 * <strong class="example">Example 3:
34 * 
35 * Input: s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
36 * Output: "aauaaaaada"
37 * 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".
38 * 
39 *  
40 * Constraints:
41 * 
42 * 	1 <= s1.length, s2.length, baseStr <= 1000
43 * 	s1.length == s2.length
44 * 	s1, s2, and baseStr consist of lowercase English letters.
45 * 
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/lexicographically-smallest-equivalent-string/
50// discuss: https://leetcode.com/problems/lexicographically-smallest-equivalent-string/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55    pub fn smallest_equivalent_string(s1: String, s2: String, base_str: String) -> String {
56        String::new()
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_1061() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.