-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
28 lines (27 loc) · 848 Bytes
/
Copy pathsolution.java
File metadata and controls
28 lines (27 loc) · 848 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
25
26
27
28
// 49. Group Anagrams
// https://leetcode.com/problems/group-anagrams/
// Medium | Java | Accepted 2024-09-05
// Runtime 6 ms | Memory 47.4 MB
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> bruh = new ArrayList<List<String>>();
Map <String, List<String>> map = new HashMap<>();
for(int i = 0; i<strs.length; i++)
{
String bruhh = strs[i];
char[] arr = bruhh.toCharArray();
Arrays.sort(arr);
String b = new String(arr);
if(!map.containsKey(b))
{
map.put(b, new ArrayList<>());
map.get(b).add(bruhh);
}
else
{
map.get(b).add(bruhh);
}
}
return new ArrayList<>(map.values());
}
}