<p>Strings <code>s1</code> and <code>s2</code> are <code>k</code><strong>-similar</strong> (for some non-negative integer <code>k</code>) if we can swap the positions of two letters in <code>s1</code> exactly <code>k</code> times so that the resulting string equals <code>s2</code>.</p>

<p>Given two anagrams <code>s1</code> and <code>s2</code>, return the smallest <code>k</code> for which <code>s1</code> and <code>s2</code> are <code>k</code><strong>-similar</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s1 = &quot;ab&quot;, s2 = &quot;ba&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> The two string are 1-similar because we can use one swap to change s1 to s2: &quot;ab&quot; --&gt; &quot;ba&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;abc&quot;, s2 = &quot;bca&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The two strings are 2-similar because we can use two swaps to change s1 to s2: &quot;abc&quot; --&gt; &quot;bac&quot; --&gt; &quot;bca&quot;.
</pre>

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

<ul>
	<li><code>1 &lt;= s1.length &lt;= 20</code></li>
	<li><code>s2.length == s1.length</code></li>
	<li><code>s1</code> and <code>s2</code> contain only lowercase letters from the set <code>{&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;}</code>.</li>
	<li><code>s2</code> is an anagram of <code>s1</code>.</li>
</ul>

# "ab" -> len(s1) == 2
from collections import deque
class Solution:
    def kSimilarity(self, s1: str, s2: str) -> int:
        # prevent double-evaluating
        seen = set()
        # approach: bfs-style evaluation
        # for every string encountered, add to queue and evaluate
        queue = deque([s1])
        swaps = 0
        # print('running')
        while queue:
            # evaluate every potential path
            levelSize = len(queue)
            for _ in range(levelSize):
                s = queue.popleft()
                seen.add(s)
                # print(s)
                if s == s2:
                    return swaps
                # make all possible swap at i
                # if s[i] != s1[i] curr doesn't match AND
                # if s[j] != s1[j] swap doesn't match AND
                # if s[j] == s1[i]
                
                # find starting point
                i = 0
                while s[i] == s2[i]:
                    i += 1
                
                # 'abcd' 'adcb'
                # attempt all valid swaps for i
                # print(f"{s}, {i}")
                for j in range(i+1, len(s)):
                    if s[j] != s2[j] and s[i] == s2[j]:
                        # create list with swapped i and j for s
                        swapped = self.swap(s, i, j)
                        # print(f"swapped {swapped}")
                        if swapped not in seen:
                            queue.append(swapped)
                # print('done processing s')
            # print('next level')
            swaps += 1
    def swap(self, s, i, j):
        s = list(s)
        temp = s[i]
        s[i] = s[j]
        s[j] = temp
        return ''.join(s)
        
        

        
        
        
        
        
        
        
### This doesn't work - too optimistic....
        # perform the swap that makes s1[0] == s2[0]
#         # recurse
#         # base case: len(s1) == len(s2)
#         def kSimilarRecursive(s1, s2, currInd, s1Ind):
#             print('running')
#             print(s1[currInd:], s2[currInd:])
#             if len(s1) != len(s2):
#                 raise ValueError(f"{s1} and {s2} are of different length; impossible to be k-similar")
#             if s1 == s2 or currInd == len(s1):
#                 # done
#                 return 0
#             elif len(s1) - currInd == 2:
#                 # base case
#                 if s1[currInd] == s2[currInd+1] and s1[currInd+1] == s2[currInd]:
#                     return 1
#                 else:
#                     raise ValueError(f"{s1[currInd:]} and {s2[currInd+1]} do not contain the same characters")
#             # find the char to swap in s2 with the min distance to its place in s1
#             targetChar = s1[currInd]
#             swapInd = currInd
#             swapTargetInds = s1Ind[s2[currInd]]
#             print(swapTargetInds)
#             minDist = float('inf')
#             for i in range(currInd, len(s2)):
#                 if s2[i] == targetChar and s2[i] != s1[i]:
#                     currDist = float('inf')
#                     for ind in swapTargetInds:
#                         currDist = abs(ind - i)
#                     if currDist < minDist:
#                         swapInd = i
#                         minDist = currDist
#             print(f"Target {s1[currInd]}. Curr {s2[currInd]}, {currInd}, Swap {s2[swapInd]}, {swapInd}")
#             # perform swap
#             if swapInd != currInd:
#                 temp = s2[currInd]
#                 s2[currInd] = s2[swapInd]
#                 s2[swapInd] = temp
#             swapRest = kSimilarRecursive(s1, s2, currInd + 1, s1Ind)
#             return 1 + swapRest if swapInd != currInd else swapRest
#         s1, s2 = list(s1), list(s2)
#         s1Ind = defaultdict(list)
#         for i in range(len(s1)):
#             s1Ind[s1[i]].append(i)
#         return kSimilarRecursive(s1, s2, 0, s1Ind)
            
                    
        
            
            
48100