Make Number of Distinct Characters Equal

You are given two 0-indexed strings word1 and word2.

A move consists of choosing two indices i and j such that 0 <= i < word1.length and 0 <= j < word2.length and swapping word1[i] with word2[j].

Return true if it is possible to get the number of distinct characters in word1 and word2 to be equal with exactly one move. Return false otherwise.

Example 1
Inputword1 = "ac", word2 = "b"
Outputfalse
Any pair of swaps would yield two distinct characters in the first string, and one in the second string.
Example 2
Inputword1 = "abcc", word2 = "aab"
Outputtrue
Swapping index 2 of the first string with index 0 of the second string yields word1 = "abac" and word2 = "cab", which both have 3 distinct characters.

Constraints

  • 1 <= word1.length, word2.length <= 10^5
  • word1 and word2 consist of only lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate