-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagrams.java
More file actions
24 lines (21 loc) · 846 Bytes
/
GroupAnagrams.java
File metadata and controls
24 lines (21 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 49. Group Anagrams
*
* Given an array of strings strs, group the anagrams together.
* You can return the answer in any order.
* An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
*/
class GroupAnagrams {
public List<List<String>> groupAnagrams(String[] theStrs) {
Map<String, List<String>> anagramMap = new HashMap<>();
for (String str : theStrs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String sortedStr = new String(chars);
if (!anagramMap.containsKey(sortedStr))
anagramMap.put(sortedStr, new ArrayList<>());
anagramMap.get(sortedStr).add(str);
}
return new ArrayList<>(anagramMap.values());
}
}