<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>

from collections import defaultdict
import string
from collections import Counter
class Solution:
    
    def insertAndRemove(self, mp, toInsert, toRemove): 
        mp[toInsert]+=1
        mp[toRemove]-=1
        
        if(mp[toRemove]==0):
            del mp[toRemove]     # if freq of that char reaches zero, then remove the key from dict
        
        
    def isItPossible(self, word1: str, word2: str) -> bool:
        
        mp1, mp2 = Counter(word1), Counter(word2)  # Get freq of chars using Counter
	
        """
        # If you are not familiar with Counters, you can simply do this:
        mp1=defaultdict(int)
        mp2=defaultdict(int)

        for w1 in word1:
            mp1[w1]+=1;   #store freq of chars in word1 in mp1

        for w2 in word2:
            mp2[w2]+=1;  #store freq of chars in word2 in mp2
        """
		
        for c1 in string.ascii_lowercase:         # this for loop iterates through c1='a' to c1='z'
            for c2 in string.ascii_lowercase:     # this for loop iterates through c2='a' to c2='z'
                
                if c1 not in mp1 or c2 not in mp2:  # if any of the char is not present then skip
                    continue

                self.insertAndRemove(mp1, c2, c1); # insert c2 to word1 and remove c1 from word1
                self.insertAndRemove(mp2, c1, c2); # insert c1 to word2 and remove c2 from word2
                
                if len(mp1)== len(mp2):  # if size of both dicts are equal then possible return True
                    return True
				
                # reset back the maps
                self.insertAndRemove(mp1, c1, c2); # insert c1 back to word1 and remove c2 from word1         
                self.insertAndRemove(mp2, c2, c1); # insert c2 back to word2 and remove c1 from word2                
        return False
133