Skip to content

Commit 1524cc4

Browse files
committed
feat: group-anagrams
1 parent 91dd214 commit 1524cc4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

group-anagrams/minji-go.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/group-anagrams/">week05-2.group-anagrams</a>
3+
* <li>Description: Given an array of strings strs, group the anagrams together</li>
4+
* <li>Topics: Array, Hash Table, String, Sorting</li>
5+
* <li>Time Complexity: O(N*KLogK), Runtime 6ms </li>
6+
* <li>Space Complexity: O(N*K), Memory 47.82MB </li>
7+
*/
8+
class Solution {
9+
public List<List<String>> groupAnagrams(String[] strs) {
10+
Map<String, List<String>> mapOfAnagrams = new HashMap<>();
11+
12+
for(String str : strs) {
13+
char[] chars = str.toCharArray();
14+
Arrays.sort(chars);
15+
String key = new String(chars);
16+
17+
List<String> anagrams = mapOfAnagrams.computeIfAbsent(key, k -> new ArrayList<>());
18+
anagrams.add(str);
19+
}
20+
21+
return new ArrayList<>(mapOfAnagrams.values());
22+
}
23+
}

0 commit comments

Comments
 (0)