-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonChars.java
51 lines (43 loc) · 1.53 KB
/
CommonChars.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
/**
* Find Common Characters
*
* Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all
* strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but
* not 4 times, you need to include that character three times in the final answer.
*
* You may return the answer in any order.
*
* e.g.
* IN: ["bella","label","roller"] OUT: ["e","l","l"]
* IN: ["cool","lock","cook"] OUT: ["c","o"]
*/
import java.util.ArrayList;
import java.util.List;
public class CommonChars {
public static void main(String[] args) {
String[] s = {"bella","label","roller"};
String[] s2 = {"cool","lock","cook"};
System.out.println(new CommonChars().commonChars(s));
System.out.println(new CommonChars().commonChars(s2));
}
public List<String> commonChars(String[] words) {
List<String> res = new ArrayList<String>();
int[] min = new int[26];
for (int i = 0; i < 26; i++) { min[i] = Integer.MAX_VALUE; }
for (String s : words) {
int[] curr = new int[26];
for (char c : s.toCharArray()) {
curr[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
min[i] = Math.min(min[i], curr[i]);
}
}
for (int i = 0; i < 26; i++) {
for (int j = 0; j < min[i]; j++) {
res.add(String.valueOf((char) (i + 'a')));
}
}
return res;
}
}