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" 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'.

Using the equivalency information from s1 and s2, return the lexicographically smallest equivalent string of baseStr.

Example 1
Inputs1 = "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
Inputs1 = "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.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate