forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_49.java
26 lines (23 loc) · 743 Bytes
/
_49.java
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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _49 {
public static class Solution1 {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String word : strs) {
char[] c = word.toCharArray();
Arrays.sort(c);
String key = new String(c);
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(word);
}
return new ArrayList<>(map.values());
}
}
}