Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 585 Bytes

859.md

File metadata and controls

32 lines (23 loc) · 585 Bytes

Buddy Strings

Description

link


Solution

  • See Code

Code

O(n)

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