Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.

Commit 1c40079

Browse files
pass all LeetCode tests
1 parent 72f8973 commit 1c40079

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

LeetCode/0859_Buddy_String.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
class Solution:
2-
def buddyStrings(self, A: str, B: str) -> bool:
3-
indexes_to_swap = []
4-
for idx, string in enumerate(A):
5-
if string != B[idx]:
6-
indexes_to_swap.append(idx)
7-
if len(indexes_to_swap) > 2:
8-
return False
9-
10-
if len(indexes_to_swap) != 2:
11-
return false
12-
13-
return A[indexes_to_swap[0]] == B[indexes_to_swap[1]] and A[indexes_to_swap[1]] == B[indexes_to_swap[0]]
1+
class Solution:
2+
@staticmethod
3+
def update_char_count(char, char_count):
4+
curr_char_count = char_count.get(char, 0)
5+
curr_char_count += 1
6+
char_count[char] = curr_char_count
7+
8+
9+
def buddyStrings(self, A: str, B: str) -> bool:
10+
char_count = {}
11+
indexes_to_swap = []
12+
dup = False
13+
for idx, string in enumerate(A):
14+
curr_char_count = char_count.get(string, 0)
15+
curr_char_count += 1
16+
char_count[string] = curr_char_count
17+
if (curr_char_count > 1):
18+
dup = True
19+
if string != B[idx]:
20+
indexes_to_swap.append(idx)
21+
if len(indexes_to_swap) > 2:
22+
return False
23+
24+
if len(indexes_to_swap) == 1:
25+
return False
26+
27+
if len(indexes_to_swap) == 2:
28+
return A[indexes_to_swap[0]] == B[indexes_to_swap[1]] and A[indexes_to_swap[1]] == B[indexes_to_swap[0]]
29+
return dup
30+
31+
print(Solution().buddyStrings("aa", "aa"))

0 commit comments

Comments
 (0)