Skip to content

Commit 7aa175a

Browse files
add 859
1 parent d9a0b06 commit 7aa175a

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Your ideas/fixes/algorithms are more than welcome!
4343
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | O(n) | O(1) | |Easy|
4444
|868|[Binary Gap](https://leetcode.com/problems/binary-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_868.java) | O(n) | O(n) | |Easy|
4545
|867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_867.java) | O(r*c) | O(r*c) | |Easy|
46+
|859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_859.java) | O(n) | O(n) | |Easy|
4647
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | O(n) | O(1) | |Easy|
4748
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
4849
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | O(n) | O(1) | |Easy|
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 859. Buddy Strings
8+
*
9+
* Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B.
10+
*
11+
*
12+
*
13+
* Example 1:
14+
*
15+
* Input: A = "ab", B = "ba"
16+
* Output: true
17+
* Example 2:
18+
*
19+
* Input: A = "ab", B = "ab"
20+
* Output: false
21+
* Example 3:
22+
*
23+
* Input: A = "aa", B = "aa"
24+
* Output: true
25+
* Example 4:
26+
*
27+
* Input: A = "aaaaaaabc", B = "aaaaaaacb"
28+
* Output: true
29+
* Example 5:
30+
*
31+
* Input: A = "", B = "aa"
32+
* Output: false
33+
*
34+
*
35+
* Note:
36+
*
37+
* 0 <= A.length <= 20000
38+
* 0 <= B.length <= 20000
39+
* A and B consist only of lowercase letters.
40+
*/
41+
public class _859 {
42+
public static class Solution1 {
43+
public boolean buddyStrings(String A, String B) {
44+
if (A.length() != B.length()) {
45+
return false;
46+
}
47+
Character c1 = null;
48+
Character c2 = null;
49+
Set<Character> set = new HashSet<>();
50+
int count = 0;
51+
for (int i = 0; i < A.length(); i++) {
52+
if (A.charAt(i) != B.charAt(i)) {
53+
if (count > 2) {
54+
return false;
55+
}
56+
if (c1 == null) {
57+
c1 = B.charAt(i);
58+
c2 = A.charAt(i);
59+
count++;
60+
continue;
61+
}
62+
if (c1 != A.charAt(i) || c2 != B.charAt(i)) {
63+
return false;
64+
}
65+
count++;
66+
}
67+
set.add(A.charAt(i));
68+
}
69+
return count == 2 || (count == 0 && set.size() < A.length());
70+
}
71+
}
72+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._859;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _859Test {
10+
private static _859.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _859.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.buddyStrings("ab", "ba"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.buddyStrings("ab", "ab"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.buddyStrings("aa", "aa"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(true, solution1.buddyStrings("aaaaaaabc", "aaaaaaacb"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(false, solution1.buddyStrings("", "aa"));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(true, solution1.buddyStrings("aaa", "aaa"));
45+
}
46+
}

0 commit comments

Comments
 (0)