Skip to content

Commit

Permalink
859
Browse files Browse the repository at this point in the history
  • Loading branch information
ProHiryu committed Jun 29, 2020
1 parent 1c4eb06 commit 2b7f3cc
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions String/859.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Buddy Strings

#### Description

[link](https://leetcode.com/problems/buddy-strings/)

---

#### Solution

- See Code

---

#### Code

O(n)

```python
class Solution:
def buddyStrings(self, A: str, B: str) -> bool:
if len(A) <= 1 or len(B) <= 1 or len(A) != len(B):
return False
if A == B:
return len(set(A)) < len(A)
i = 0
while A[i] == B[i]: i += 1
for j in range(i + 1, len(A)):
if A[j] == B[i] and A[i] == B[j]:
A = A[:i] + A[j] + A[i+1:j] + A[i] + A[j+1:]
return A == B
```

0 comments on commit 2b7f3cc

Please sign in to comment.