> For the complete documentation index, see [llms.txt](https://snowan.gitbook.io/study-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://snowan.gitbook.io/study-notes/leetcode/python/000859-buddy-strings.md).

# 859-Buddy-Strings

### Problem

<https://leetcode.com/problems/buddy-strings/description/>

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

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

For example, swapping at indices 0 and 2 in "abcd" results in "cbad".

Example 1:

Input: s = "ab", goal = "ba" Output: true Explanation: You can swap s\[0] = 'a' and s\[1] = 'b' to get "ba", which is equal to goal. Example 2:

Input: s = "ab", goal = "ab" Output: false Explanation: The only letters you can swap are s\[0] = 'a' and s\[1] = 'b', which results in "ba" != goal. Example 3:

Input: s = "aa", goal = "aa" Output: true Explanation: You can swap s\[0] = 'a' and s\[1] = 'a' to get "aa", which is equal to goal.

Constraints:

1 <= s.length, goal.length <= 2 \* 104 s and goal consist of lowercase letters.

### Solution

```python
class Solution:
    def buddyStrings(self, s: str, goal: str) -> bool:
        # 1. not same length or len < 2, return False
        # 2. if s == goal, check whether there are any repeatable letter, if yes, True, otherwise False
        # 3. for other cases, check diff letters, if len(diffs) >= 3, False. otherwise, pair[0] == reverse[pair[1]]
        if len(s) != len(goal):
            return False
        if s == goal:
            return len(set(s)) < len(s)
        diffs = [(a, b) for a, b in zip(s, goal) if a != b]
        return len(diffs) == 2 and diffs[0] == diffs[1][::-1]

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://snowan.gitbook.io/study-notes/leetcode/python/000859-buddy-strings.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
