-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path1002.Find-Common-Characters.java
More file actions
40 lines (35 loc) · 992 Bytes
/
1002.Find-Common-Characters.java
File metadata and controls
40 lines (35 loc) · 992 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
35
36
37
38
39
40
// https://leetcode.com/problems/find-common-characters/
//
// algorithms
// Easy (66.61%)
// Total Accepted: 14,803
// Total Submissions: 22,225
// beats 95.22% of java submissions
class Solution {
public List<String> commonChars(String[] A) {
int ch[] = new int[26];
Arrays.fill(ch, 101);
for (String s : A) {
int tmp[] = new int[26];
for (char c : s.toCharArray()) {
tmp[c - 'a']++;
}
getSmaller(ch, tmp);
}
List<String> res = new ArrayList<>();
for (int i = 0; i < 26; i++) {
String s = String.valueOf((char)(i + 'a'));
for (int j = 0; j < ch[i]; j++) {
res.add(s);
}
}
return res;
}
public void getSmaller(int[] ch, int[] b) {
for (int i = 0; i < 26; i++) {
if (ch[i] > b[i]) {
ch[i] = b[i];
}
}
}
}