<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 &lt;= i &lt; word1.length</code> and <code>0 &lt;= j &lt; 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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;ac&quot;, word2 = &quot;b&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> Any pair of swaps would yield two distinct characters in the first string, and one in the second string.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;abcc&quot;, word2 = &quot;aab&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> We swap index 2 of the first string with index 0 of the second string. The resulting strings are word1 = &quot;abac&quot; and word2 = &quot;cab&quot;, which both have 3 distinct characters.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;abcde&quot;, word2 = &quot;fghij&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> Both resulting strings will have 5 distinct characters, regardless of which indices we swap.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li>
	<li><code>word1</code> and <code>word2</code> consist of only lowercase English letters.</li>
</ul>

class Solution:
    def isItPossible(self, word1: str, word2: str) -> bool:
        dct1 = collections.Counter(word1)
        dct2 = collections.Counter(word2)

        for c1 in list(dct1.keys()):
            for c2 in list(dct2.keys()):
                self.swap(dct1, c1, c2)
                self.swap(dct2, c2, c1)
                # print(dct1, dct2)
                if len(dct1) == len(dct2):
                    return True
                self.swap(dct1, c2, c1)
                self.swap(dct2, c1, c2)

        return False

    def swap(self, dct, c1, c2):
        if dct[c1] == 1:
            del dct[c1]
        else:
            dct[c1] -= 1

        if c2 not in dct:
            dct[c2] = 1
        else:
            dct[c2] += 1

    def fun1(self, word1: str, word2: str) -> bool:

        c1 = collections.Counter(max(word1, word2, key=lambda x: len(set(x))))
        c2 = collections.Counter(min(word2, word1, key=lambda x: len(set(x))))

        sc1 = set(c1.keys())
        sc2 = set(c2.keys())

        sc11 = set(k for k, v in c1.items() if v == 1)
        sc21 = set(k for k, v in c2.items() if v == 1)

        sc1n = set(k for k, v in c1.items() if v > 1)
        sc2n = set(k for k, v in c2.items() if v > 1)

        lc1 = len(sc1)
        lc2 = len(sc2)
        if lc1 - lc2 >= 3:
            return False
        if lc1 - lc2 == 2:
            s1 = sc11 - sc2
            s2 = sc2n & sc1
            if s1 and s2 and s1 != s2:
                return True
            else:
                return False

        if lc1 - lc2 == 1:
            # n + 1
            if (sc1n - sc2) and (sc2n & sc1):
                return True

            if (sc11 - sc2) and (sc2n - sc1):
                return True

            # n

            if (sc11 - sc21) and (sc2 - sc1n) and sc11 and sc1n:
                return True

            # n
            if (sc11 - sc2) and (sc21 & sc1):
                return True

            if (sc11 & sc2) and (sc2n & sc1):
                return True

            return False

        if lc1 == lc2:
            if not (sc1 & sc2) and (
                (len(sc11) == 0 and len(sc2n) == 0) or (len(sc1n) == 0 and len(sc21) == 0)
            ):
                return False
            return True


121