Class Solution
java.lang.Object
g1601_1700.s1657_determine_if_two_strings_are_close.Solution
1657 - Determine if Two Strings Are Close.<p>Medium</p>
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>abcde -> aecdb</code></li>
</ul>
</li>
<li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character.
<ul>
<li>For example, <code>aacabb -> bbcbaa</code> (all <code>a</code>’s turn into <code>b</code>’s, and all <code>b</code>’s turn into <code>a</code>’s)</li>
</ul>
</li>
</ul>
<p>You can use the operations on either string as many times as necessary.</p>
<p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code> <em>if</em> <code>word1</code> <em>and</em> <code>word2</code> <em>are <strong>close</strong> , and</em> <code>false</code> <em>otherwise.</em></p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> word1 = “abc”, word2 = “bca”</p>
<p><strong>Output:</strong> true</p>
<p><strong>Explanation:</strong> You can attain word2 from word1 in 2 operations.</p>
<p>Apply Operation 1: “abc” -> “acb”</p>
<p>Apply Operation 1: “acb” -> “bca”</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> word1 = “a”, word2 = “aa”</p>
<p><strong>Output:</strong> false</p>
<p><strong>Explanation:</strong> It is impossible to attain word2 from word1, or vice versa, in any number of operations.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> word1 = “cabbba”, word2 = “abbccc”</p>
<p><strong>Output:</strong> true</p>
<p><strong>Explanation:</strong> You can attain word2 from word1 in 3 operations.</p>
<p>Apply Operation 1: “cabbba” -> “caabbb”</p>
<p><code>Apply Operation 2: "</code>caabbb" -> “baaccc”</p>
<p>Apply Operation 2: “baaccc” -> “abbccc”</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= word1.length, word2.length <= 10<sup>5</sup></code></li>
<li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
closeStrings
-