------
## 最终结论
```python
from collections import deque

def k_similarity(A: str, B: str) -> int:
    if A == B:
        return 0

    queue = deque([(A, 0)])  # (current string, number of swaps)
    visited = {A}

    while queue:
        current, swaps = queue.popleft()

        # Generate all possible states by swapping characters
        for i in range(len(current)):
            if current[i] != B[i]:  # Only consider swapping if the characters are different
                for j in range(i + 1, len(current)):
                    if current[j] != B[j] and current[j] == B[i]:  # Ensure we only swap with a matching character
                        # Swap characters at position i and j
                        new_state = list(current)
                        new_state[i], new_state[j] = new_state[j], new_state[i]
                        new_state = ''.join(new_state)

                        if new_state == B:  # Check if the new state matches target string B
                            return swaps + 1

                        if new_state not in visited:
                            visited.add(new_state)
                            queue.append((new_state, swaps + 1))
                break  # Only need to fix one position at a time

# Example usage:
print(k_similarity("ab", "ba"))   # Output: 1
print(k_similarity("abc", "bca")) # Output: 2
print(k_similarity("abac", "baca")) # Output: 2
print(k_similarity("aabc", "abca")) # Output: 2
```