Skip to content

Commit 93943bb

Browse files
committed
Added a solution
1 parent cdb1985 commit 93943bb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Easy/Buddy Strings.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public static boolean buddyStrings(String A, String B) {
3+
if (A.length() != B.length() || A.length() <= 1 || B.length() <= 1) {
4+
return false;
5+
}
6+
7+
if (A.equals(B)) {
8+
Set<Character> s = new HashSet<Character>();
9+
for (char c : A.toCharArray()) {
10+
s.add(c);
11+
}
12+
13+
return s.size() < A.length();
14+
}
15+
16+
List<Integer> dif = new ArrayList<>();
17+
for (int i = 0; i < A.length(); ++i) {
18+
if (A.charAt(i) != B.charAt(i)) {
19+
dif.add(i);
20+
}
21+
}
22+
23+
return dif.size() == 2 && A.charAt(dif.get(0)) == B.charAt(dif.get(1)) && A.charAt(dif.get(1)) == B.charAt(dif.get(0));
24+
}
25+
}

0 commit comments

Comments
 (0)