You are given two strings word1 and word2. Your goal is to equalize the number of distinct characters in both strings using exactly one character swap between them.
A move consists of choosing any character at position i in word1 and any character at position j in word2, then swapping them. You must determine if it's possible to make both strings have the same number of unique characters with exactly one such swap.
Example: If word1 = "ac" has 2 distinct characters and word2 = "b" has 1 distinct character, can we swap one character to make them equal?
Return true if such a swap exists, false otherwise.
Input & Output
Visualization
Time & Space Complexity
Single pass to count frequencies, then O(1) analysis for each unique character pair
Fixed size arrays for character frequencies (26 letters max)
Constraints
- 1 โค word1.length, word2.length โค 105
- word1 and word2 consist only of lowercase English letters
- Exactly one swap must be performed