<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:
        # word1 = "aac"
        # word2 = "cca" -> True

        # word1 = "abcc", word2 = "aba"
        # word1 has 3 DC, word2 has 2 DC

        
        # count distinct characters of word1 in a hashmap
        # count distanct characters of word2 in a hashmap
        word1_counts = [0] * 26
        word2_counts = [0] * 26
        alphabet = "abcdefghijklmnopqrstuvwxyz"
        # Time complexity : O(N) Space Complexity: O(26 * 2) -> O(52)
        for letter in word1:
            word1_counts[alphabet.find(letter)] += 1
        for letter in word2:
            word2_counts[alphabet.find(letter)] += 1
            # word2[letter] = word2[letter] + 1
        for i in range(len(word1_counts)):
            for j in range(len(word2_counts)):
                # if eitherof the values are 0, we can't swap, because there's nothing to swap, continue in that case
                if word1_counts[i] == 0 or word2_counts[j] == 0:
                    continue
                # index into the other array and add it
                word1_counts[i] -= 1
                word1_counts[j] += 1
                word2_counts[j] -= 1
                word2_counts[i] += 1
                word1_dc, word2_dc = 0, 0
                # loop through both arrays and get the counts of distinct characters, and the way we count it is if value is non zero
                for ind in range(26):
                    if word1_counts[ind] > 0:
                        word1_dc += 1
                    if word2_counts[ind] > 0:
                        word2_dc += 1
                # compare, return if True
                if word1_dc == word2_dc:
                    return True
                # lastly, we need to revert our changes
                word1_counts[i] += 1
                word1_counts[j] -= 1
                word2_counts[j] += 1
                word2_counts[i] -= 1



        # word1_counts = {a : 2, b : 0, c : 2} -> 2
        # word2_counts = {a : 1, b : 2} -> 1

        # word1_counts = {a : 0, c : 1, b : 1}
        # word2_counts = {b : 0, a : 1}
        return False

        # Brute Force
        # for every letter in word1
            # for every letter in word2
                # swap them
                # check if equal


245