File tree 5 files changed +91
-0
lines changed
5 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,7 @@ All solutions will be accepted!
191
191
| 633| [ Sum Of Square Numbers] ( https://leetcode-cn.com/problems/sum-of-square-numbers/description/ ) | [ java/py/js] ( ./algorithms/SumOfSquareNumbers ) | Easy|
192
192
| 605| [ Can Place Flowers] ( https://leetcode-cn.com/problems/can-place-flowers/description/ ) | [ java/py/js] ( ./algorithms/CanPlaceFlowers ) | Easy|
193
193
| 204| [ Count Primes] ( https://leetcode-cn.com/problems/count-primes/description/ ) | [ java/py/js] ( ./algorithms/CountPrimes ) | Easy|
194
+ | 859| [ Buddy Strings] ( https://leetcode-cn.com/problems/buddy-strings/description/ ) | [ java/py/js] ( ./algorithms/BuddyStrings ) | Easy|
194
195
195
196
# Database
196
197
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Buddy Strings
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean buddyStrings (String A , String B ) {
3
+ if (A .length () != B .length ()) return false ;
4
+
5
+ if (A .equals (B )) {
6
+ Map <Character , Boolean > characterMap = new HashMap <Character , Boolean >();
7
+ for (char c : A .toCharArray ()) {
8
+ if (characterMap .get (c ) == null ) {
9
+ characterMap .put (c , true );
10
+ } else {
11
+ return true ;
12
+ }
13
+ }
14
+ return false ;
15
+ } else {
16
+ List <Character > diffs = new ArrayList <Character >();
17
+ for (int i = 0 ; i < A .length (); i ++) {
18
+ if (A .charAt (i ) != B .charAt (i )) {
19
+ diffs .add (A .charAt (i ));
20
+ diffs .add (B .charAt (i ));
21
+ }
22
+ }
23
+ if (diffs .size () == 4 && diffs .get (0 ) == diffs .get (3 ) && diffs .get (1 ) == diffs .get (2 )) return true ;
24
+ return false ;
25
+ }
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } A
3
+ * @param {string } B
4
+ * @return {boolean }
5
+ */
6
+ var buddyStrings = function ( A , B ) {
7
+ if ( A . length !== B . length ) return false
8
+
9
+ if ( A === B ) {
10
+ let characterMap = { }
11
+ for ( let i = 0 ; i < A . length ; i ++ ) {
12
+ if ( characterMap [ A [ i ] ] === undefined ) {
13
+ characterMap [ A [ i ] ] = true
14
+ } else {
15
+ return true
16
+ }
17
+ }
18
+ return false
19
+ } else {
20
+ let diffs = [ ]
21
+ for ( let i = 0 ; i < A . length ; i ++ ) {
22
+ if ( A [ i ] !== B [ i ] ) {
23
+ diffs . push ( A [ i ] )
24
+ diffs . push ( B [ i ] )
25
+ }
26
+ }
27
+ if ( diffs . length === 4 && diffs [ 0 ] === diffs [ 3 ] && diffs [ 1 ] == diffs [ 2 ] ) return true
28
+ return false
29
+ }
30
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def buddyStrings (self , A , B ):
3
+ """
4
+ :type A: str
5
+ :type B: str
6
+ :rtype: bool
7
+ """
8
+ if len (A ) != len (B ):
9
+ return False
10
+
11
+ if A == B :
12
+ # 只要有一个重复的字母即可
13
+ character_map = {}
14
+ for c in A :
15
+ if character_map .get (c ) == None :
16
+ character_map [c ] = True
17
+ else :
18
+ return True
19
+ return False
20
+ else :
21
+ # 只能有2个位置不同,且a1 == b2 and b1 == a2
22
+ diffs = []
23
+ for i in range (len (A )):
24
+ if A [i ] != B [i ]:
25
+ diffs .append (A [i ])
26
+ diffs .append (B [i ])
27
+ if len (diffs ) == 4 and diffs [0 ] == diffs [3 ] and diffs [1 ] == diffs [2 ]:
28
+ return True
29
+ return False
30
+
31
+
You can’t perform that action at this time.
0 commit comments