-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1002-FindCommonCharacters.java
More file actions
36 lines (31 loc) · 965 Bytes
/
Copy path1002-FindCommonCharacters.java
File metadata and controls
36 lines (31 loc) · 965 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
class Solution {
public List<String> commonChars(String[] words) {
List<String> result = new ArrayList<>();
int n = words.length;
int[] arr = new int[26];
String firstWord = words[0];
for (int i = 0; i < firstWord.length(); i++) {
arr[firstWord.charAt(i) - 'a']++;
}
for (int i = 1; i < words.length; i++) {
int[] temp = new int[26];
for (char c : words[i].toCharArray()) {
if (arr[c - 'a'] == 0)
continue;
else {
temp[c - 'a']++;
arr[c - 'a']--;
}
}
arr = temp;
}
for (int i = 0; i < arr.length; i++) {
while (arr[i] > 0) {
char value = (char) (i + 'a');
result.add(String.valueOf(value));
arr[i]--;
}
}
return result;
}
}