-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindCommonCharacters1002.java
More file actions
31 lines (29 loc) · 966 Bytes
/
FindCommonCharacters1002.java
File metadata and controls
31 lines (29 loc) · 966 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
import java.util.*;
public class FindCommonCharacters1002 {
public List<String> commonChars(String[] words) {
List<String> result = new ArrayList<>();
int[] arr = new int[26];
Arrays.fill(arr, Integer.MAX_VALUE);
for (String word : words) {
int[] temp = new int[26];
for (char c : word.toCharArray()) {
temp[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
arr[i] = Math.min(arr[i], temp[i]);
}
}
for (int i = 0; i < 26; i++) {
while (arr[i] > 0) {
result.add("" + (char) ('a' + i));
arr[i]--;
}
}
return result;
}
public static void main(String[] args) {
FindCommonCharacters1002 obj = new FindCommonCharacters1002();
String[] words = { "bella", "label", "roller" };
System.out.println(obj.commonChars(words));
}
}