-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuddyString.java
34 lines (28 loc) · 956 Bytes
/
BuddyString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package string;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
//859 https://leetcode.com/problems/buddy-strings/
public class BuddyString {
public boolean buddyStrings(String A, String B) {
if(A.length()!=B.length()) return false;
if(A.equals(B)){
Set<Character> chars= new HashSet<>();
for(int i=0;i<A.length();i++){
chars.add(A.charAt(i));
}
return chars.size()<A.length();
}
List<Integer> diffCount=new ArrayList<>();
for(int i=0;i<A.length();i++){
if(A.charAt(i)!=B.charAt(i) )
diffCount.add(i);
}
if(diffCount.size()>2)
return false;
return diffCount.size()==2
&& A.charAt(diffCount.get(0))== B.charAt(diffCount.get(1))
&& B.charAt(diffCount.get(0))== A.charAt(diffCount.get(1));
}
}