-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1002.java
More file actions
55 lines (50 loc) · 1.73 KB
/
1002.java
File metadata and controls
55 lines (50 loc) · 1.73 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
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public List<String> commonChars(String[] A) {
List<String> resultList = new ArrayList<>();
int result[] = new int[26];
for(char c : A[0].toCharArray()){
result[c-'a']++;
}
for(int i=1;i<A.length;i++){
updateChar(result,A[i]);
}
for(int i=0;i<result.length;i++){
while(result[i] > 0){
resultList.add((char) (i+'a') + "");
result[i]--;
}
}
return resultList;
}
private void updateChar(int arr[],String s){
int result[] = new int[26];
for(char c : s.toCharArray()){
result[c-'a']++;
}
for(int i=0;i<result.length;i++){
if(result[i] < arr[i])
arr[i]=result[i];
}
}
}
__________________________________________________________________________________________________
sample 35600 kb submission
class Solution {
public List<String> commonChars(String[] A) {
int[][] freq = new int[26][A.length];
List<String> result = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
for (char c : A[i].toCharArray()) {
freq[c - 'a'][i]++;
}
}
for (int i = 0; i < 26; i++) {
int common = Arrays.stream(freq[i]).min().getAsInt();
for (int k = 0; k < common; k++) result.add(String.valueOf((char)(i + 'a')));
}
return result;
}
}
__________________________________________________________________________________________________