File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments