-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1002.java
More file actions
44 lines (41 loc) · 1.43 KB
/
LeetCode1002.java
File metadata and controls
44 lines (41 loc) · 1.43 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LeetCode1002 {
public static void main(String[] args) {
// 输入:words = ["bella","label","roller"]
// 输出:["e","l","l"]
System.out
.println(Arrays.toString(
(new Solution1002().commonChars(new String[] { "bella", "label", "roller" }).toArray())));
// 输入:words = ["cool","lock","cook"]
// 输出:["c","o"]
System.out
.println(Arrays.toString(
(new Solution1002().commonChars(new String[] { "cool", "lock", "cook" }).toArray())));
}
}
class Solution1002 {
public List<String> commonChars(String[] words) {
int[] minFreq = new int[26];
Arrays.fill(minFreq, Integer.MAX_VALUE);
int[] freq;
for (String word : words) {
freq = new int[26];
for (int i = 0; i < word.length(); i++) {
freq[word.charAt(i) - 'a']++;
}
for (int i = 0; i < 26; i++) {
minFreq[i] = Math.min(freq[i], minFreq[i]);
}
}
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < 26; i++) {
int count = minFreq[i];
for (int j = 0; j < count; j++) {
result.add(String.valueOf((char) (i + 'a')));
}
}
return result;
}
}