We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cdb1985 commit 93943bbCopy full SHA for 93943bb
Easy/Buddy Strings.java
@@ -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