|
| 1 | +<h2><a href="https://leetcode.com/problems/lexicographically-smallest-equivalent-string/?envType=daily-question&envId=2025-06-05">1061. Lexicographically Smallest Equivalent String</a></h2><h3>Medium</h3><hr><p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> |
| 2 | + |
| 3 | +<p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>For example, if <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>Equivalent characters follow the usual rules of any equivalence relation:</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li> |
| 13 | + <li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li> |
| 14 | + <li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p>For example, given the equivalency information from <code>s1 = "abc"</code> and <code>s2 = "cde"</code>, <code>"acd"</code> and <code>"aab"</code> are equivalent strings of <code>baseStr = "eed"</code>, and <code>"aab"</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p> |
| 18 | + |
| 19 | +<p>Return <em>the lexicographically smallest equivalent string of </em><code>baseStr</code><em> by using the equivalency information from </em><code>s1</code><em> and </em><code>s2</code>.</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> s1 = "parker", s2 = "morris", baseStr = "parser" |
| 26 | +<strong>Output:</strong> "makkek" |
| 27 | +<strong>Explanation:</strong> Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i]. |
| 28 | +The characters in each group are equivalent and sorted in lexicographical order. |
| 29 | +So the answer is "makkek". |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong class="example">Example 2:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> s1 = "hello", s2 = "world", baseStr = "hold" |
| 36 | +<strong>Output:</strong> "hdld" |
| 37 | +<strong>Explanation: </strong>Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r]. |
| 38 | +So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld". |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong class="example">Example 3:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> s1 = "leetcode", s2 = "programs", baseStr = "sourcecode" |
| 45 | +<strong>Output:</strong> "aauaaaaada" |
| 46 | +<strong>Explanation:</strong> 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". |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | +<p><strong>Constraints:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li> |
| 54 | + <li><code>s1.length == s2.length</code></li> |
| 55 | + <li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li> |
| 56 | +</ul> |
0 commit comments