-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_1002.java
More file actions
34 lines (30 loc) · 905 Bytes
/
Copy pathP_1002.java
File metadata and controls
34 lines (30 loc) · 905 Bytes
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
package leetcode.easy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SuppressWarnings("MethodParameterNamingConvention")
public class P_1002 {
public List<String> commonChars(String[] A) {
final List<String> res = new ArrayList<>();
final int[] map = new int[26];
Arrays.fill(map, Integer.MAX_VALUE);
for (String word : A) {
updateMap(map, word);
}
for (int i = 0; i < 26; i++) {
while (map[i]-- > 0) {
res.add(String.valueOf((char) (i + 'a')));
}
}
return res;
}
private static void updateMap(int[] map, String s) {
final int[] sMap = new int[26];
for (char c : s.toCharArray()) {
sMap[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
map[i] = Math.min(sMap[i], map[i]);
}
}
}