- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1002.java
39 lines (37 loc) · 1.24 KB
/
_1002.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
31
32
33
34
35
36
37
38
39
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayList;
importjava.util.List;
publicclass_1002 {
publicstaticclassSolution1 {
publicList<String> commonChars(String[] A) {
int[][] charCount = newint[A.length][26];
for (inti = 0; i < A.length; i++) {
for (charc : A[i].toCharArray()) {
charCount[i][c - 'a']++;
}
}
List<String> result = newArrayList<>();
for (inti = 0; i < 26; i++) {
while (charCount[0][i] != 0) {
charc = (char) (i + 'a');
booleanvalid = true;
charCount[0][i]--;
for (intj = 1; j < A.length; j++) {
if (charCount[j][i] == 0) {
valid = false;
break;
} else {
charCount[j][i]--;
}
}
if (!valid) {
break;
} else {
result.add("" + c);
}
}
}
returnresult;
}
}
}