java.lang.Object
g2501_2600.s2531_make_number_of_distinct_characters_equal.Solution

public class Solution extends Object
2531 - Make Number of Distinct Characters Equal.<p>Medium</p> <p>You are given two <strong>0-indexed</strong> strings <code>word1</code> and <code>word2</code>.</p> <p>A <strong>move</strong> consists of choosing two indices <code>i</code> and <code>j</code> such that <code>0 <= i < word1.length</code> and <code>0 <= j < word2.length</code> and swapping <code>word1[i]</code> with <code>word2[j]</code>.</p> <p>Return <code>true</code> <em>if it is possible to get the number of distinct characters in</em> <code>word1</code> <em>and</em> <code>word2</code> <em>to be equal with <strong>exactly one</strong> move.</em> Return <code>false</code> <em>otherwise</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;ac&rdquo;, word2 = &ldquo;b&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> Any pair of swaps would yield two distinct characters in the first string, and one in the second string.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;abcc&rdquo;, word2 = &ldquo;aab&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> We swap index 2 of the first string with index 0 of the second string. The resulting strings are word1 = &ldquo;abac&rdquo; and word2 = &ldquo;cab&rdquo;, which both have 3 distinct characters.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;abcde&rdquo;, word2 = &ldquo;fghij&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> Both resulting strings will have 5 distinct characters, regardless of which indices we swap.</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> consist of only lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • isItPossible

      public boolean isItPossible(String word1, String word2)