LeetCode - Algorithms - 1002. Find Common Characters

Problem

1002. Find Common Characters

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
class Solution {
public List<String> commonChars(String[] A) {
List<String> clist = new ArrayList<String>();
final int N = A.length;
int[][] counter = new int[26][N];

for (int i = 0; i < N; i++) {
for (int j = 0; j < A[i].length(); j++) {
counter[A[i].charAt(j) - 'a'][i]++;
}
}

int m;
String s = "";
for (int i = 0; i < 26; i++) {
m = counter[i][0];
for (int j = 1; j < N; j++) {
if (counter[i][j] < m)
m = counter[i][j];
}
if (m > 0) {
s = (char)(i + 'a') + "";
for (; m > 0; m--) {
clist.add(s);
}
}
}
return clist;
}
}

Submission Detail

  • 83 / 83 test cases passed.
  • Runtime: 7 ms, faster than 53.85% of Java online submissions for Find Common Characters.
  • Memory Usage: 39.4 MB, less than 7.83% of Java online submissions for Find Common Characters.