Skip to content

Commit 34b8696

Browse files
add one more solution for 893
1 parent b7baf91 commit 34b8696

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/main/java/com/fishercoder/solutions/_893.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.fishercoder.solutions;
22

33
import java.util.Arrays;
4-
import java.util.HashMap;
54
import java.util.HashSet;
6-
import java.util.Map;
75
import java.util.Set;
86
import java.util.stream.Collectors;
97

@@ -40,7 +38,7 @@
4038
* */
4139
public class _893 {
4240
public static class Solution1 {
43-
/**my original solution, a bit length:
41+
/**my original solution, a bit lengthy:
4442
* generate a unique signaure as key for each equivelant group and sum them up*/
4543
public int numSpecialEquivGroups(String[] A) {
4644
return Arrays.stream(A).map(this::getCommonKey).collect(Collectors.toSet()).size();
@@ -62,4 +60,29 @@ private String getCommonKey(String word) {
6260
return new StringBuffer().append(new String(evenIndexed)).append(new String(oddIndexed)).toString();
6361
}
6462
}
63+
64+
public static class Solution2 {
65+
/**
66+
* more concise solution: https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163413/Java-Concise-Set-Solution
67+
* but somehow a bit slower than mine: 12 ms vs 7ms
68+
* I guess due to the problem constraint and this: "1 <= A[i].length <= 20" to have made this problem simpler
69+
*/
70+
public int numSpecialEquivGroups(String[] A) {
71+
Set<String> set = new HashSet<>();
72+
for (String str : A) {
73+
int[] odd = new int[26];
74+
int[] even = new int[26];
75+
for (int i = 0; i < str.length(); i++) {
76+
if (i % 2 == 0) {
77+
even[str.charAt(i) - 'a']++;
78+
} else {
79+
odd[str.charAt(i) - 'a']++;
80+
}
81+
}
82+
String key = Arrays.toString(even) + Arrays.toString(odd);
83+
set.add(key);
84+
}
85+
return set.size();
86+
}
87+
}
6588
}

src/test/java/com/fishercoder/_893Test.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,30 @@
88

99
public class _893Test {
1010
private static _893.Solution1 solution1;
11+
private static _893.Solution2 solution2;
1112

1213
@BeforeClass
1314
public static void setup() {
1415
solution1 = new _893.Solution1();
16+
solution2 = new _893.Solution2();
1517
}
1618

1719
@Test
1820
public void test1() {
1921
assertEquals(3, solution1.numSpecialEquivGroups(new String[]{"abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"}));
22+
assertEquals(3, solution2.numSpecialEquivGroups(new String[]{"abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"}));
2023
}
2124

2225
@Test
2326
public void test2() {
2427
assertEquals(3, solution1.numSpecialEquivGroups(new String[]{"abc", "acb", "bac", "bca", "cab", "cba"}));
28+
assertEquals(3, solution2.numSpecialEquivGroups(new String[]{"abc", "acb", "bac", "bca", "cab", "cba"}));
2529
}
2630

2731
@Test
2832
public void test3() {
2933
assertEquals(1, solution1.numSpecialEquivGroups(new String[]{"abcd", "cdab", "adcb", "cbad"}));
34+
assertEquals(1, solution2.numSpecialEquivGroups(new String[]{"abcd", "cdab", "adcb", "cbad"}));
3035
}
3136

3237
}

0 commit comments

Comments
 (0)