Lexicographically Smallest Equivalent String Problem
Description
LeetCode Problem 1061.
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'.
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:
Input: s1 = "parker", s2 = "morris", baseStr = "parser"
Output: "makkek"
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
The characters in each group are equivalent and sorted in lexicographical order.
So the answer is "makkek".
Example 2:
Input: s1 = "hello", s2 = "world", baseStr = "hold"
Output: "hdld"
Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
Example 3:
Input: s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
Output: "aauaaaaada"
Explanation: We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
Constraints:
1 <= s1.length, s2.length, baseStr <= 1000s1.length == s2.lengths1,s2, andbaseStrconsist of lowercase English letters.
Solution
Approach: Union-Find with the Smallest Letter as Root
Each pair s1[i], s2[i] says two letters are equivalent, and the equivalence is transitive, so the pairs split the 26 letters into groups. Every letter of baseStr can be replaced by any letter in its group, and since letters are independent of each other, the smallest string replaces each letter by the smallest letter of its group.
Union-find (disjoint sets) over the 26 letters builds the groups. The one twist is how to merge: when uniting two groups, always hang the root with the larger letter under the root with the smaller letter. Then the root of every group is its smallest letter, and find(c) directly gives the replacement for c.
For example s1 = "abc", s2 = "cde" unites a with c, b with d, then c (root a) with e, giving the groups {a, c, e} and {b, d}, so baseStr = "eed" becomes "aab".
Complexity: O((n + m) · α) time, where n is the length of s1, m the length of baseStr, and α the nearly constant union-find factor; O(1) extra space besides the answer (26 parent entries).
class Solution:
def smallestEquivalentString(self, s1: str, s2: str, baseStr: str) -> str:
parent = list(range(26)) # union-find over the letters; each root is the smallest letter of its group
def find(x: int) -> int:
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
for a, b in zip(s1, s2):
ra, rb = find(ord(a) - ord("a")), find(ord(b) - ord("a"))
# Hang the larger root under the smaller one.
if ra < rb:
parent[rb] = ra
else:
parent[ra] = rb
return "".join(chr(find(ord(c) - ord("a")) + ord("a")) for c in baseStr)