-
Notifications
You must be signed in to change notification settings - Fork 20
/
GroupAnagrams.java
58 lines (49 loc) · 1.28 KB
/
GroupAnagrams.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.techmisal.medium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
Given an array of strings, group anagrams together.
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
All inputs will be in lowercase.
The order of your output does not matter.
*/
public class GroupAnagrams {
public List<List<String>> groupAnagrams(String[] strs) {
Map<Map<Character, Integer>, ArrayList<String>> map = new HashMap<>();
for (int i = 0; i < strs.length; i++) {
Map<Character, Integer> mapOfS = getFreqMap(strs[i]);
if(map.containsKey(mapOfS)) {
ArrayList<String> value = map.get(mapOfS);
value.add(strs[i]);
map.put(mapOfS, value);
} else {
ArrayList<String> list = new ArrayList<>();
list.add(strs[i]);
map.put(mapOfS, list);
}
}
return Arrays.asList(map.values().toArray(new ArrayList[0]));
}
public Map<Character, Integer> getFreqMap(String s) {
Map<Character, Integer> mapOfS = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char chs = s.charAt(i);
if (mapOfS.containsKey(chs)) {
mapOfS.put(chs, mapOfS.get(chs) + 1);
} else {
mapOfS.put(chs, 1);
}
}
return mapOfS;
}
}