Skip to content

Commit 32bd02c

Browse files
add 1657
1 parent 977b69c commit 32bd02c

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1657|[Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1657.java) ||Medium|Greedy|
1112
|1656|[Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1656.java) ||Easy|Array, Design|
1213
|1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|
1314
|1640|[Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1640.java) ||Easy|Array, Sort|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class _1657 {
8+
public static class Solution1 {
9+
public boolean closeStrings(String word1, String word2) {
10+
int[] counts1 = new int[26];
11+
int[] counts2 = new int[26];
12+
Set<Character> set1 = new HashSet<>();
13+
Set<Character> set2 = new HashSet<>();
14+
for (char c : word1.toCharArray()) {
15+
counts1[c - 'a']++;
16+
set1.add(c);
17+
}
18+
Arrays.sort(counts1);
19+
for (char c : word2.toCharArray()) {
20+
counts2[c - 'a']++;
21+
set2.add(c);
22+
}
23+
Arrays.sort(counts2);
24+
return set1.equals(set2) && Arrays.equals(counts1, counts2);
25+
}
26+
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1657;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1657Test {
10+
private static _1657.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1657.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.closeStrings("abc", "bca"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.closeStrings("a", "aa"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.closeStrings("cabbba", "abbccc"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(false, solution1.closeStrings("cabbba", "aabbss"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(false, solution1.closeStrings("abbbzcf", "babzzcz"));
40+
}
41+
}

0 commit comments

Comments
 (0)