-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupAnagCopy.java
More file actions
38 lines (33 loc) · 1.32 KB
/
groupAnagCopy.java
File metadata and controls
38 lines (33 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
public class groupAnagCopy {
public static List<List<String>> groupAnagrams(String[] strs) {
HashMap<String,List<String>> map = new HashMap<>();
for (String word : strs) {
char[] wordArr = word.toCharArray();
Arrays.sort(wordArr);
// create new string; don't use toString()
String sortedWord = new String(wordArr);
System.out.println("11sorted: " + sortedWord);
if (!map.containsKey(sortedWord)) {
map.put(sortedWord, new ArrayList<>());
}
map.get(sortedWord).add(word);
System.out.println("15: Map" + map);
}
return new ArrayList<>(map.values());
}
public static void main(String[] args) {
String[] strs1 = {"eat","tea","tan","ate","nat","bat"};
System.out.println("Output1: " + groupAnagrams(strs1));
String[] strs2 = {""};
System.out.println("Output2: " + groupAnagrams(strs2));
String[] strs3 = {"a"};
System.out.println("Output3: " + groupAnagrams(strs3));
}
}
/* return list of strings
key will be sorted word, vals will be list: ate: eat, ate, tea
Take in string: convert to char array, sort it, stringify
if the sorted word is not in map, add it
add the word (original) to the sorted word's list
*/