Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3d241cc

Browse files
refactor 49
1 parent cd250cd commit 3d241cc

File tree

3 files changed

+62
-43
lines changed

3 files changed

+62
-43
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ repositories {
2222
dependencies {
2323
compile 'com.google.code.gson:gson:2.8.0'
2424
compile 'junit:junit:4.12'
25+
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.0'
2526
testCompile group: 'junit', name: 'junit', version:'4.12'
2627
}

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,35 @@
1010
* 49. Group Anagrams
1111
*
1212
* Given an array of strings, group anagrams together.
13-
14-
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
15-
Return:
16-
17-
[
18-
["ate", "eat","tea"],
19-
["nat","tan"],
20-
["bat"]
21-
]
22-
23-
Note: All inputs will be in lower-case.
13+
*
14+
* For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
15+
* Return:
16+
* [
17+
* ["ate", "eat","tea"],
18+
* ["nat","tan"],
19+
* ["bat"]
20+
* ]
21+
*
22+
* Note:
23+
* All inputs will be in lowercase.
24+
* The order of your output does not matter.
2425
*/
2526

2627
public class _49 {
2728

28-
public static class Solution1 {
29-
public List<List<String>> groupAnagrams(String[] strs) {
30-
Map<String, List<String>> map = new HashMap<>();
31-
for (String word : strs) {
32-
char[] c = word.toCharArray();
33-
Arrays.sort(c);
34-
String key = new String(c);
35-
if (!map.containsKey(key)) {
36-
map.put(key, new ArrayList<>());
29+
public static class Solution1 {
30+
public List<List<String>> groupAnagrams(String[] strs) {
31+
Map<String, List<String>> map = new HashMap<>();
32+
for (String word : strs) {
33+
char[] c = word.toCharArray();
34+
Arrays.sort(c);
35+
String key = new String(c);
36+
if (!map.containsKey(key)) {
37+
map.put(key, new ArrayList<>());
38+
}
39+
map.get(key).add(word);
40+
}
41+
return new ArrayList<>(map.values());
3742
}
38-
map.get(key).add(word);
39-
}
40-
return new ArrayList<>(map.values());
4143
}
42-
}
4344
}
Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._49;
4+
45
import java.util.ArrayList;
56
import java.util.Arrays;
67
import java.util.List;
8+
9+
import org.apache.commons.collections4.CollectionUtils;
710
import org.junit.BeforeClass;
811
import org.junit.Test;
912

1013
import static junit.framework.TestCase.assertEquals;
14+
import static junit.framework.TestCase.assertTrue;
1115

1216
public class _49Test {
13-
private static _49.Solution1 solution1;
14-
private static String[] words;
15-
private static List<List<String>> expected;
16-
private static List<List<String>> actual;
17+
private static _49.Solution1 solution1;
18+
private static String[] words;
19+
private static List<List<String>> expected;
20+
private static List<List<String>> actual;
1721

18-
@BeforeClass
19-
public static void setup() {
20-
solution1 = new _49.Solution1();
21-
}
22+
@BeforeClass
23+
public static void setup() {
24+
solution1 = new _49.Solution1();
25+
}
2226

23-
@Test
24-
public void test1() {
25-
words = new String[] {"eat", "tea", "tan", "ate", "nat", "bat"};
26-
expected = new ArrayList<>();
27-
expected.add(Arrays.asList("ate", "eat", "tea"));
28-
expected.add(Arrays.asList("nat", "tan"));
29-
expected.add(Arrays.asList("bat"));
30-
actual = solution1.groupAnagrams(words);
31-
assertEquals(expected.size(), actual.size());
32-
assertEquals(expected.containsAll(actual), actual.containsAll(expected));
33-
}
27+
@Test
28+
public void test1() {
29+
words = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
30+
List<String> e1 = Arrays.asList("bat");
31+
List<String> e2 = Arrays.asList("tan", "nat");
32+
List<String> e3 = Arrays.asList("ate", "eat", "tea");
33+
expected = Arrays.asList(e1, e2, e3);
34+
actual = solution1.groupAnagrams(words);
35+
assertEquals(expected.size(), actual.size());
36+
assertEquals(expected.containsAll(actual), actual.containsAll(expected));
37+
for (List<String> a : actual) {
38+
switch (a.size()) {
39+
case 1:
40+
assertTrue(CollectionUtils.isEqualCollection(e1, a));
41+
break;
42+
case 2:
43+
assertTrue(CollectionUtils.isEqualCollection(e2, a));
44+
break;
45+
case 3:
46+
assertTrue(CollectionUtils.isEqualCollection(e3, a));
47+
break;
48+
}
49+
}
50+
}
3451
}

0 commit comments

Comments
 (0)