This repository was archived by the owner on Sep 22, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +31
-13
lines changed Expand file tree Collapse file tree 1 file changed +31
-13
lines changed Original file line number Diff line number Diff line change 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" ))
You can’t perform that action at this time.
0 commit comments