Lexicographically Smallest Equivalent String - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: s1 = "abc", s2 = "cde", baseStr = "eed"
Output: "aab"
💡 Note: Equivalencies: 'a'=='c', 'b'=='d', 'c'=='e'. By transitivity: 'a'=='c'=='e', so group {a,c,e} has root 'a'. Group {b,d} has root 'b'. So "eed" becomes "aab".
Example 2 — Self Equivalency
$ Input: s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
Output: "aauaaaaada"
💡 Note: Multiple character equivalencies create several groups. Each character in baseStr is replaced by its group's lexicographically smallest character.
Example 3 — No Changes
$ Input: s1 = "a", s2 = "b", baseStr = "xyz"
Output: "xyz"
💡 Note: Only 'a' and 'b' are equivalent. Characters 'x', 'y', 'z' in baseStr remain unchanged since they have no equivalencies.

Constraints

  • 1 ≤ s1.length, s2.length, baseStr.length ≤ 1000
  • s1.length == s2.length
  • s1, s2, and baseStr consist of lowercase English letters.

Visualization

Tap to expand
Lexicographically Smallest Equivalent String INPUT s1 = "abc" s2 = "cde" baseStr = "eed" Equivalence Pairs: a = c b = d c = e Equivalence Graph: a b c d e baseStr: e e d ALGORITHM STEPS 1 Initialize Union-Find parent[i] = i for all chars 2 Union Equivalent Chars Union(a,c), Union(b,d), Union(c,e) 3 Always Pick Smallest Make smaller char the root in each union operation 4 Transform baseStr Replace each char with its smallest equivalent Final Parent Array: a,c,e --> root: a b,d --> root: b FINAL RESULT Character Mapping: e --> a (smallest) e --> a (smallest) d --> b (smallest) Output String: a a b Answer: "aab" OK - Verified Key Insight: Use Union-Find (Disjoint Set Union) to group equivalent characters. The key optimization is to always make the lexicographically smaller character the root during union operations. This way, finding the root of any character automatically gives us the smallest equivalent character. TutorialsPoint - Lexicographically Smallest Equivalent String | Optimal Solution (Union-Find)
Asked in
Google 25 Amazon 18 Facebook 15
32.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen