leetcode-1002. 查找常用字符

题解思路

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
class Solution {
public List<String> commonChars(String[] A) {
List<String> result = new ArrayList<>();
int n = A.length;
int[] ans = new int[26];
Arrays.fill(ans, 1000000000);

for (String str : A) {
int[] tmp = new int[26];
for (int i = 0; i < str.length(); i++) {
tmp[str.charAt(i) - 'a'] += 1;
}

for (int i = 0; i < 26; i++) {
ans[i] = Math.min(ans[i], tmp[i]);
}
}

for (int i = 0; i < 26; i++) {
while (ans[i] != 0) {
result.add(String.valueOf((char) ('a' + i)));
ans[i] -= 1;
}
}

return result;
}
}