There was an error while loading. Please reload this page.
1 parent d127b37 commit 8de32fcCopy full SHA for 8de32fc
Strings/49_Group_Anagrams.java
@@ -0,0 +1,23 @@
1
+class Solution {
2
+ public List<List<String>> groupAnagrams(String[] strs) {
3
+ if (strs == null || strs.length == 0) {
4
+ return Collections.emptyList();
5
+ }
6
+
7
+ Map<String, List<String>> hm = new HashMap<>();
8
9
+ for (String s : strs) {
10
+ char[] sArr = s.toCharArray();
11
+ Arrays.sort(sArr);
12
13
+ String keyStr = String.valueOf(sArr);
14
+ if (!hm.containsKey(keyStr)) {
15
+ hm.put(keyStr, new ArrayList<>());
16
17
18
+ hm.get(keyStr).add(s);
19
20
21
+ return new ArrayList<List<String>>(hm.values());
22
23
+}
0 commit comments