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

47 lines
1.7 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<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>