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

#define ll long long
#define pb push_back
class Solution {
public:
    ll f(ll index,string & s1, string & s2,ll n,map<pair<ll,string>,ll>& memo){
        if(memo.find({index,s1})!=memo.end()){
            return memo[{index,s1}];
        }
        if(index==n)return memo[{index,s1}]=0;
        if(s1[index]==s2[index]){
            return memo[{index,s1}]=f(index+1,s1,s2,n,memo);
        }

        ll mini = 1e9;
        for(ll i=index+1;i<n;i++){
            if(s2[index]==s1[i]){
                swap(s1[index],s1[i]);
                mini=min(mini,f(index+1,s1,s2,n,memo)+1);
                swap(s1[index],s1[i]);
            }
        }
        return memo[{index,s1}]=mini;
    }
    int kSimilarity(string s1, string s2) {
        ll n = s1.size();
        map<pair<ll,string>,ll>memo;
        ll ans = f(0,s1,s2,n,memo);
        return ans;
    }
};
116253