We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3af456c commit 01df823Copy full SHA for 01df823
group-anagrams/sora0319.java
@@ -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