Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
pass all LeetCode tests
  • Loading branch information
barrotsteindev committed Oct 31, 2020
commit 1c40079195ffe5f055e098dc12c20a65b7d3fbd6
44 changes: 31 additions & 13 deletions LeetCode/0859_Buddy_String.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
class Solution:
def buddyStrings(self, A: str, B: str) -> bool:
indexes_to_swap = []
for idx, string in enumerate(A):
if string != B[idx]:
indexes_to_swap.append(idx)
if len(indexes_to_swap) > 2:
return False

if len(indexes_to_swap) != 2:
return false

return A[indexes_to_swap[0]] == B[indexes_to_swap[1]] and A[indexes_to_swap[1]] == B[indexes_to_swap[0]]
class Solution:
@staticmethod
def update_char_count(char, char_count):
curr_char_count = char_count.get(char, 0)
curr_char_count += 1
char_count[char] = curr_char_count


def buddyStrings(self, A: str, B: str) -> bool:
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

print(Solution().buddyStrings("aa", "aa"))