forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup Anagrams.java
More file actions
executable file
·170 lines (121 loc) · 4.2 KB
/
Group Anagrams.java
File metadata and controls
executable file
·170 lines (121 loc) · 4.2 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
M
方法一: 60%
和check anagram 想法一样:转化并sort char array,用来作为key。
把所有anagram 存在一起。注意结尾Collections.sort().
O(NKlog(K)), N = string[] length, k = longest word length
优化:80% ~ 97%
用固定长度的char[26] arr 存每个字母的frequency; 然后再 new string(arr).
因为每个位子上的frequency的变化,就能构建一个unique的string
错误的示范: 尝试先sort input strs[],但是NlogN 其实效率更低. 13%
```
/*
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return:
[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note:
For the return value, each inner list's elements must follow the lexicographic order.
All inputs will be in lower-case.
Hide Company Tags Amazon Bloomberg Uber Facebook
Hide Tags Hash Table String
Hide Similar Problems (E) Valid Anagram (E) Group Shifted Strings
*/
/*
optmize1:
Use collectoins.sort() instead of Arrays.sort() in front
分析:
n: str[].length
p: parts: number of keys in hashmap
m: max string length
(n/p): average ['abc', 'cab', 'cba', ... etc] length
Collections.sort(...) : (n/p)log(n/p) * p = n*log(n/p)
so, how large is n/p?
1. p is small, -> result goes large
2. p is large -> result goes Small
overal:
n*m + n*log(n/p)
If Arrays.sort(strs) at first:
n*m + nlogn
Therefore, using Collections.sort() in this problem is faster than using Arrays.sort() in front.
optimize2:
char[26] arr: [1,1,2,0,0,0,0,0,0,0,...]
new String(arr) -> key of hashmap
n * m
optimize3:
If not necessary, we don't have to use map.entrySet();
we can just use map.keySet(); It's faster
for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
Collections.sort(entry.getValue());
rst.add(entry.getValue());
}
LEET CODE SPEED: 80% ~ 97% (25ms ~ 22ms)
*/
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> rst = new ArrayList<List<String>>();
if (strs == null || strs.length == 0) {
return rst;
}
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for (int i = 0; i < strs.length; i++) {
String str = calcUniqueKey(strs[i]);
if (!map.containsKey(str)) {
map.put(str, new ArrayList<String>());
}
map.get(str).add(strs[i]);
}
for(String key: map.keySet()){//FASTER
Collections.sort(map.get(key));
rst.add(map.get(key));
}
return rst;
}
public String calcUniqueKey(String s) {
char[] arr = new char[26];
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i) - 'a'] += 1;
}
return new String(arr);
}
}
/*
Thoughts:
brutle force: save to Map<String, List> O(n) space,time
Store the anagram in a order list. Collections.sort it. MO(logM)
Note: use Arrays.sort() to sort string.
Note2: can do (element : array, arraylist) in for loop
*/
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> rst = new ArrayList<List<String>>();
if (strs == null || strs.length == 0) {
return rst;
}
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for (int i = 0; i < strs.length; i++) {
char[] arr = strs[i].toCharArray();
Arrays.sort(arr);
String str = new String(arr);
if (!map.containsKey(str)) {
map.put(str, new ArrayList<String>());
}
map.get(str).add(strs[i]);
}
/*
for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {//SLOW
Collections.sort(entry.getValue());
rst.add(entry.getValue());
}
*/
for(String key: map.keySet()){//FASTER
Collections.sort(map.get(key));
rst.add(map.get(key));
}
return rst;
}
}
```