Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9fd4028

Browse files
authored
Create 49. Group Anagrams.java
1 parent 8740b98 commit 9fd4028

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

java/49. Group Anagrams.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
HashMap<String, List<String>> map = new HashMap<>();
3+
4+
public List<List<String>> groupAnagrams(String[] strs) {
5+
6+
for (int idx=0;idx<strs.length;idx++){
7+
process(strs[idx]);
8+
}
9+
10+
return new ArrayList<List<String>>(map.values());
11+
}
12+
13+
public void process(String data){
14+
int[] az= new int[26];
15+
16+
for (int idx=0;idx<data.length();idx++){
17+
az[data.charAt(idx)-'a'] += 1;
18+
}
19+
StringBuilder result = new StringBuilder();
20+
for (int idx=0;idx<26;idx++){
21+
if (az[idx] == 0) continue;
22+
23+
result.append(idx+"-"+az[idx]);
24+
}
25+
String out = result.toString();
26+
if (!map.containsKey(out)) map.put(out, new ArrayList());
27+
28+
map.get(out).add(data);
29+
}
30+
}

0 commit comments

Comments
 (0)