<p>Given two strings <code>s</code> and <code>goal</code>, return <code>true</code><em> if you can swap two letters in </em><code>s</code><em> so the result is equal to </em><code>goal</code><em>, otherwise, return </em><code>false</code><em>.</em></p>

<p>Swapping letters is defined as taking two indices <code>i</code> and <code>j</code> (0-indexed) such that <code>i != j</code> and swapping the characters at <code>s[i]</code> and <code>s[j]</code>.</p>

<ul>
	<li>For example, swapping at indices <code>0</code> and <code>2</code> in <code>&quot;abcd&quot;</code> results in <code>&quot;cbad&quot;</code>.</li>
</ul>

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

<pre>
<strong>Input:</strong> s = &quot;ab&quot;, goal = &quot;ba&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> You can swap s[0] = &#39;a&#39; and s[1] = &#39;b&#39; to get &quot;ba&quot;, which is equal to goal.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;ab&quot;, goal = &quot;ab&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> The only letters you can swap are s[0] = &#39;a&#39; and s[1] = &#39;b&#39;, which results in &quot;ba&quot; != goal.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;aa&quot;, goal = &quot;aa&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> You can swap s[0] = &#39;a&#39; and s[1] = &#39;a&#39; to get &quot;aa&quot;, which is equal to goal.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length, goal.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>s</code> and <code>goal</code> consist of lowercase letters.</li>
</ul>

class Solution {
public:
    bool buddyStrings(string s, string goal) {
        if (s.length() != goal.length())
            return false; // 如果长度不同，则不可能相等

        if (s == goal) { // 如果字符串已经相等
            // 检查字符串中是否有重复字符
            vector<int> freq(26, 0);
            for (char c : s) {
                freq[c - 'a']++;
                if (freq[c - 'a'] > 1) // 如果有任何字符出现超过一次
                    return true;
            }
            return false; // 如果没有重复字符，不能交换使字符串不同
        }

        // 如果字符串不相等，继续检查
        char c1 = ' ', c2 = ' '; // 用于存储第一个不相等字符对的字符
        int mismatchCount = 0; // 记录不相等字符对的数量

        for (int i = 0; i < s.length(); i++) {
            if (s[i] != goal[i]) {
                mismatchCount++;

                // 如果不相等的字符对数量超过两个，返回 false
                if (mismatchCount > 2)
                    return false;

                if (c1 == ' ') {  // 第一次发现不相等的字符对
                    c1 = s[i];    // 存储 s 中的字符
                    c2 = goal[i]; // 存储 goal 中的字符
                } else {
                    // 检查第二个不相等的字符对是否能通过交换匹配
                    if (c1 != goal[i] || c2 != s[i])
                        return false;
                }
            }
        }

        // 如果有正好两个不相等的位置，返回 true
        return mismatchCount == 2;
    }
};
8100