Skip to content

Commit 01df823

Browse files
committed
solve group anagrams
1 parent 3af456c commit 01df823

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

group-anagrams/sora0319.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
Map<String, List<String>> groups = new HashMap<>();
4+
5+
for(String word : strs){
6+
int[] characters = new int[26];
7+
for(char c : word.toCharArray()){
8+
characters[c - 'a']++;
9+
}
10+
11+
String countCode = Arrays.toString(characters);
12+
if(!groups.containsKey(countCode)){
13+
groups.put(countCode, new ArrayList<String>());
14+
}
15+
List<String> temp = groups.get(countCode);
16+
temp.add(word);
17+
}
18+
19+
List<List<String>> answer = new ArrayList<>();
20+
for(List<String> g : groups.values()){
21+
answer.add(g);
22+
}
23+
24+
return answer;
25+
}
26+
}
27+

0 commit comments

Comments
 (0)