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.
- For example, if
s1 = "abc"ands2 = "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'.
Using the equivalency information from s1 and s2, return the lexicographically smallest equivalent string of baseStr.
Example 1
Input
s1 = "parker", s2 = "morris", baseStr = "parser"Output
"makkek"Based on the equivalency information in s1 and s2, the character groups are [m,p], [a,o], [k,r,s], and [e,i], so the lexicographically smallest equivalent string is "makkek".
Example 2
Input
s1 = "hello", s2 = "world", baseStr = "hold"Output
"hdld"Based on the equivalency information in s1 and s2, the character groups are [h,w], [d,e,o], and [l,r], so only the second letter 'o' in baseStr changes to 'd'.
Constraints
- 1 <= s1.length, s2.length, baseStr <= 1000
- s1.length == s2.length
- s1, s2, and baseStr consist of lowercase English letters.