1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/交换字符串中的元素 [smallest-string-with-swaps].html

47 lines
1.7 KiB
HTML
Raw Normal View History

2022-03-27 20:37:52 +08:00
<p>给你一个字符串&nbsp;<code>s</code>,以及该字符串中的一些「索引对」数组&nbsp;<code>pairs</code>,其中&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;表示字符串中的两个索引(编号从 0 开始)。</p>
<p>你可以 <strong>任意多次交换</strong>&nbsp;<code>pairs</code>&nbsp;中任意一对索引处的字符。</p>
<p>返回在经过若干次交换后,<code>s</code>&nbsp;可以变成的按字典序最小的字符串。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre><strong>输入:</strong>s = &quot;dcab&quot;, pairs = [[0,3],[1,2]]
<strong>输出:</strong>&quot;bacd&quot;
<strong>解释:</strong>
交换 s[0] 和 s[3], s = &quot;bcad&quot;
交换 s[1] 和 s[2], s = &quot;bacd&quot;
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]]
<strong>输出:</strong>&quot;abcd&quot;
<strong>解释:</strong>
交换 s[0] 和 s[3], s = &quot;bcad&quot;
交换 s[0] 和 s[2], s = &quot;acbd&quot;
交换 s[1] 和 s[2], s = &quot;abcd&quot;</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>s = &quot;cba&quot;, pairs = [[0,1],[1,2]]
<strong>输出:</strong>&quot;abc&quot;
<strong>解释:</strong>
交换 s[0] 和 s[1], s = &quot;bca&quot;
交换 s[1] 和 s[2], s = &quot;bac&quot;
交换 s[0] 和 s[1], s = &quot;abc&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
<li><code>0 &lt;= pairs.length &lt;= 10^5</code></li>
<li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li>
<li><code>s</code>&nbsp;中只含有小写英文字母</li>
</ul>