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)/确定两个字符串是否接近 [determine-if-two-strings-are-close].html
2022-03-29 12:43:11 +08:00

66 lines
2.7 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>如果可以使用以下操作从一个字符串得到另一个字符串,则认为两个字符串 <strong>接近</strong> </p>
<ul>
<li>操作 1交换任意两个 <strong>现有</strong> 字符。
<ul>
<li>例如,<code>a<strong>b</strong>cd<strong>e</strong> -> a<strong>e</strong>cd<strong>b</strong></code></li>
</ul>
</li>
<li>操作 2将一个 <strong>现有</strong> 字符的每次出现转换为另一个 <strong>现有</strong> 字符,并对另一个字符执行相同的操作。
<ul>
<li>例如,<code><strong>aa</strong>c<strong>abb</strong> -> <strong>bb</strong>c<strong>baa</strong></code>(所有 <code>a</code> 转化为 <code>b</code> ,而所有的 <code>b</code> 转换为 <code>a</code> </li>
</ul>
</li>
</ul>
<p>你可以根据需要对任意一个字符串多次使用这两种操作。</p>
<p>给你两个字符串,<code>word1</code><code>word2</code> 。如果<em> </em><code>word1</code><em> </em><em> </em><code>word2</code><em> </em><strong>接近 </strong>,就返回 <code>true</code> ;否则,返回<em> </em><code>false</code><em> </em></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>word1 = "abc", word2 = "bca"
<strong>输出:</strong>true
<strong>解释:</strong>2 次操作从 word1 获得 word2 。
执行操作 1"a<strong>bc</strong>" -> "a<strong>cb</strong>"
执行操作 1"<strong>a</strong>c<strong>b</strong>" -> "<strong>b</strong>c<strong>a</strong>"
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>word1 = "a", word2 = "aa"
<strong>输出:</strong>false
<strong>解释:</strong>不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>word1 = "cabbba", word2 = "abbccc"
<strong>输出:</strong>true
<strong>解释:</strong>3 次操作从 word1 获得 word2 。
执行操作 1"ca<strong>b</strong>bb<strong>a</strong>" -> "ca<strong>a</strong>bb<strong>b</strong>"
执行操作 2<code>"</code><strong>c</strong>aa<strong>bbb</strong>" -> "<strong>b</strong>aa<strong>ccc</strong>"
执行操作 2"<strong>baa</strong>ccc" -> "<strong>abb</strong>ccc"
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>word1 = "cabbba", word2 = "aabbss"
<strong>输出:</strong>false
<strong>解释:</strong>不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= word1.length, word2.length <= 10<sup>5</sup></code></li>
<li><code>word1</code><code>word2</code> 仅包含小写英文字母</li>
</ul>