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.
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:
'a' == 'a'.'a' == 'b' implies 'b' == 'a'.'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, and baseStr consist of lowercase English letters.The idea is to use a union-find data structure to record the equivalence relationships between characters. The union operation will merge sets of equivalent characters.
Next, for each character, we find its representative in the equivalence class (or set), and map each character to the lexicographically smallest character in its set, thus ensuring the smallest equivalent transformation.
This implementation initializes a union-find structure to keep track of the parent of each character (where each unique character starts as its own parent). By performing union operations on pairs of characters from s1 and s2, we merge equivalent sets.
In the final step, we find the parent of each character in baseStr and construct the smallest equivalent string.
C++
Java
Python
C#
JavaScript
Time Complexity: O(N + M), where N is the length of s1 and s2, and M is the length of baseStr.
Space Complexity: O(ALPHABET_SIZE), which is constant due to the fixed size of the alphabet.
Another method is treating each character as a node in a graph where edges connect equivalent nodes (characters). Perform Depth-First Search (DFS) to find all characters that belong to an equivalence group.
Once we have gathered all groups and established the smallest character for each group, we can form the desired output by mapping each baseStr character to the smallest in its equivalence class.
This Python function constructs a graph from the equivalences given in s1 and s2. It then performs DFS to determine the connected components of the graph, which represent equivalence classes. After identifying each group, it maps all characters in the group to their minimum character to construct the smallest equivalent string for baseStr.
Time Complexity: O(N + M), where N is the length of s1 and s2, and M is the number of edges in the graph (size of baseStr).
Space Complexity: O(ALPHABET_SIZE), due to the storage of graph nodes and mappings.
| Approach | Complexity |
|---|---|
| Union-Find Approach | Time Complexity: O(N + M), where N is the length of Space Complexity: O(ALPHABET_SIZE), which is constant due to the fixed size of the alphabet. |
| DFS Graph Approach | Time Complexity: O(N + M), where N is the length of Space Complexity: O(ALPHABET_SIZE), due to the storage of graph nodes and mappings. |
Leetcode 1061. Lexicographically Smallest Equivalent String - Disjoint Set / Union-Find • Code-Yao • 2,742 views views
Watch 9 more video solutions →Practice Lexicographically Smallest Equivalent String with our built-in code editor and test cases.
Practice on FleetCode