java.lang.Object
g1601_1700.s1657_determine_if_two_strings_are_close.Solution

public class Solution extends Object
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>&rsquo;s turn into <code>b</code>&rsquo;s, and all <code>b</code>&rsquo;s turn into <code>a</code>&rsquo;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 = &ldquo;abc&rdquo;, word2 = &ldquo;bca&rdquo;</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: &ldquo;abc&rdquo; -> &ldquo;acb&rdquo;</p> <p>Apply Operation 1: &ldquo;acb&rdquo; -> &ldquo;bca&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;a&rdquo;, word2 = &ldquo;aa&rdquo;</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 = &ldquo;cabbba&rdquo;, word2 = &ldquo;abbccc&rdquo;</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: &ldquo;cabbba&rdquo; -> &ldquo;caabbb&rdquo;</p> <p><code>Apply Operation 2: &quot;</code>caabbb&quot; -> &ldquo;baaccc&rdquo;</p> <p>Apply Operation 2: &ldquo;baaccc&rdquo; -> &ldquo;abbccc&rdquo;</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 Details

    • Solution

      public Solution()
  • Method Details

    • closeStrings

      public boolean closeStrings(String word1, String word2)