Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions LeetCode/0859_Buddy_String.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def buddyStrings(self, A: str, B: str) -> bool:
if len(A) != len(B):
return False
char_count = {}
indexes_to_swap = []
dup = False
for idx, string in enumerate(A):
curr_char_count = char_count.get(string, 0)
curr_char_count += 1
char_count[string] = curr_char_count
if (curr_char_count > 1):
dup = True
if string != B[idx]:
indexes_to_swap.append(idx)
if len(indexes_to_swap) > 2:
return False

if len(indexes_to_swap) == 1:
return False

if len(indexes_to_swap) == 2:
return A[indexes_to_swap[0]] == B[indexes_to_swap[1]] and A[indexes_to_swap[1]] == B[indexes_to_swap[0]]
return dup