Imagine you have a character translation system where certain characters are considered equivalent to each other. You're given two strings s1 and s2 of the same length that define these equivalencies: s1[i] and s2[i] are equivalent characters.
These equivalencies follow standard mathematical rules:
- Reflexivity:
'a' == 'a' - Symmetry: If
'a' == 'b', then'b' == 'a' - Transitivity: If
'a' == 'b'and'b' == 'c', then'a' == 'c'
Your goal: Transform the given baseStr into its lexicographically smallest equivalent string by replacing each character with the smallest equivalent character from its equivalency group.
Example: If s1 = "abc" and s2 = "cde", then 'a' ↔ 'c', 'b' ↔ 'd', and 'c' ↔ 'e'. Due to transitivity, 'a' ↔ 'c' ↔ 'e', so all three belong to the same group. For baseStr = "eed", the result would be "aad".
Input & Output
Visualization
Time & Space Complexity
For each character in baseStr (n), we might traverse all 26 letters in worst case
Adjacency list stores at most 26 characters and their connections
Constraints
- 1 ≤ s1.length, s2.length ≤ 1000
- s1.length == s2.length
- 1 ≤ baseStr.length ≤ 1000
- All strings contain only lowercase English letters