java.lang.Object
g1201_1300.s1202_smallest_string_with_swaps.Solution

public class Solution extends Object
1202 - Smallest String With Swaps.<p>Medium</p> <p>You are given a string <code>s</code>, and an array of pairs of indices in the string <code>pairs</code> where <code>pairs[i] = [a, b]</code> indicates 2 indices(0-indexed) of the string.</p> <p>You can swap the characters at any pair of indices in the given <code>pairs</code> <strong>any number of times</strong>.</p> <p>Return the lexicographically smallest string that <code>s</code> can be changed to after using the swaps.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;dcab&rdquo;, pairs = [[0,3],[1,2]]</p> <p><strong>Output:</strong> &ldquo;bacd&rdquo; <strong>Explaination:</strong></p> <p>Swap s[0] and s[3], s = &ldquo;bcad&rdquo;</p> <p>Swap s[1] and s[2], s = &ldquo;bacd&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;dcab&rdquo;, pairs = [[0,3],[1,2],[0,2]]</p> <p><strong>Output:</strong> &ldquo;abcd&rdquo; <strong>Explaination:</strong></p> <p>Swap s[0] and s[3], s = &ldquo;bcad&rdquo;</p> <p>Swap s[0] and s[2], s = &ldquo;acbd&rdquo;</p> <p>Swap s[1] and s[2], s = &ldquo;abcd&rdquo;</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;cba&rdquo;, pairs = [[0,1],[1,2]]</p> <p><strong>Output:</strong> &ldquo;abc&rdquo; <strong>Explaination:</strong></p> <p>Swap s[0] and s[1], s = &ldquo;bca&rdquo;</p> <p>Swap s[1] and s[2], s = &ldquo;bac&rdquo;</p> <p>Swap s[0] and s[1], s = &ldquo;abc&rdquo;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10^5</code></li> <li><code>0 <= pairs.length <= 10^5</code></li> <li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li> <li><code>s</code> only contains lower case English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details