forked from Google-DSC-TMSL/ProjectAlgorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagramsProg.java
More file actions
Latest commit
31 lines (26 loc) · 986 Bytes
/
Copy pathGroupAnagramsProg.java
File metadata and controls
31 lines (26 loc) · 986 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
29
30
31
// GROUP ANAGRAMS (LEETCODE 49) SOLUTION
// Question : https://leetcode.com/problems/group-anagrams/
//--------------------------------------------------------------------------
classSolution {
publicList<List<String>> groupAnagrams(String[] strs) {
if (strs.length == 0)
returnnewArrayList();
Map<String, List> ans = newHashMap<String, List>();
int[] count = newint[26];
for (Strings : strs) {
Arrays.fill(count, 0);
for (charc : s.toCharArray())
count[c - 'a']++;
StringBuildersb = newStringBuilder("");
for (inti = 0; i < 26; i++) {
sb.append('#');
sb.append(count[i]);
}
Stringkey = sb.toString();
if (!ans.containsKey(key))
ans.put(key, newArrayList());
ans.get(key).add(s);
}
returnnewArrayList(ans.values());
}
}