Class Solution
java.lang.Object
g1201_1300.s1202_smallest_string_with_swaps.Solution
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 = “dcab”, pairs = [[0,3],[1,2]]</p>
<p><strong>Output:</strong> “bacd” <strong>Explaination:</strong></p>
<p>Swap s[0] and s[3], s = “bcad”</p>
<p>Swap s[1] and s[2], s = “bacd”</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> s = “dcab”, pairs = [[0,3],[1,2],[0,2]]</p>
<p><strong>Output:</strong> “abcd” <strong>Explaination:</strong></p>
<p>Swap s[0] and s[3], s = “bcad”</p>
<p>Swap s[0] and s[2], s = “acbd”</p>
<p>Swap s[1] and s[2], s = “abcd”</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> s = “cba”, pairs = [[0,1],[1,2]]</p>
<p><strong>Output:</strong> “abc” <strong>Explaination:</strong></p>
<p>Swap s[0] and s[1], s = “bca”</p>
<p>Swap s[1] and s[2], s = “bac”</p>
<p>Swap s[0] and s[1], s = “abc”</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 Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
smallestStringWithSwaps
-