java.lang.Object
g1001_1100.s1061_lexicographically_smallest_equivalent_string.Solution

public class Solution extends Object
1061 - Lexicographically Smallest Equivalent String.<p>Medium</p> <p>You are given two strings of the same length <code>s1</code> and <code>s2</code> and a string <code>baseStr</code>.</p> <p>We say <code>s1[i]</code> and <code>s2[i]</code> are equivalent characters.</p> <ul> <li>For example, if <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, then we have <code>'a' == 'c'</code>, <code>'b' == 'd'</code>, and <code>'c' == 'e'</code>.</li> </ul> <p>Equivalent characters follow the usual rules of any equivalence relation:</p> <ul> <li><strong>Reflexivity:</strong> <code>'a' == 'a'</code>.</li> <li><strong>Symmetry:</strong> <code>'a' == 'b'</code> implies <code>'b' == 'a'</code>.</li> <li><strong>Transitivity:</strong> <code>'a' == 'b'</code> and <code>'b' == 'c'</code> implies <code>'a' == 'c'</code>.</li> </ul> <p>For example, given the equivalency information from <code>s1 = &quot;abc&quot;</code> and <code>s2 = &quot;cde&quot;</code>, <code>&quot;acd&quot;</code> and <code>&quot;aab&quot;</code> are equivalent strings of <code>baseStr = &quot;eed&quot;</code>, and <code>&quot;aab&quot;</code> is the lexicographically smallest equivalent string of <code>baseStr</code>.</p> <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> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s1 = &ldquo;parker&rdquo;, s2 = &ldquo;morris&rdquo;, baseStr = &ldquo;parser&rdquo;</p> <p><strong>Output:</strong> &ldquo;makkek&rdquo;</p> <p><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].</p> <p>The characters in each group are equivalent and sorted in lexicographical order.</p> <p>So the answer is &ldquo;makkek&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s1 = &ldquo;hello&rdquo;, s2 = &ldquo;world&rdquo;, baseStr = &ldquo;hold&rdquo;</p> <p><strong>Output:</strong> &ldquo;hdld&rdquo;</p> <p><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].</p> <p>So only the second letter &lsquo;o&rsquo; in baseStr is changed to &lsquo;d&rsquo;, the answer is &ldquo;hdld&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s1 = &ldquo;leetcode&rdquo;, s2 = &ldquo;programs&rdquo;, baseStr = &ldquo;sourcecode&rdquo;</p> <p><strong>Output:</strong> &ldquo;aauaaaaada&rdquo;</p> <p><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 &lsquo;u&rsquo; and &lsquo;d&rsquo; are transformed to &lsquo;a&rsquo;, the answer is &ldquo;aauaaaaada&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li> <li><code>s1.length == s2.length</code></li> <li><code>s1</code>, <code>s2</code>, and <code>baseStr</code> consist of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details