Medium
,String
,Union Find
1061. Lexicographically Smallest Equivalent String
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.
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:
'a' == 'a'
.'a' == 'b'
implies 'b' == 'a'
.'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:
Example 2:
Example 3:
Constraints:
s1.length
, s2.length
, baseStr
<= 1000s1.length
== s2.length
s1
, s2
, and baseStr
consist of lowercase English letters.XD Jan 14, 2023
Time:
Extra Space:
Yen-Chi ChenSat, Jan 14, 2023
Yen-Chi ChenSat, Jan 14, 2023
Ron ChenWed, Jan 18, 2023