-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_Common_Characters.java
More file actions
49 lines (46 loc) · 1.26 KB
/
Find_Common_Characters.java
File metadata and controls
49 lines (46 loc) · 1.26 KB
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
41
42
43
44
45
46
47
48
49
import java.util.*;
import java.io.*;
import java.lang.*;
public class Find_Common_Characters {
class Solution {
public List<String> commonChars(String[] words)
{
int[] last = count(words[0]);
for(int i = 1; i < words.length; i++)
{
last = intersection(last, count(words[i]));
}
List<String> arr = new ArrayList<>();
for(int i = 0; i < 26; i++)
{
if(last[i] != 0)
{
char a = (char) ('a' + i);
String s = String.valueOf(a);
while(last[i] > 0)
{
arr.add(s);
last[i]--;
}
}
}
return arr;
}
private int[] intersection(int[] a, int[] b) {
int[] t = new int[26];
for(int i = 0; i < 26; i++)
{
t[i] = Math.min(a[i], b[i]);
}
return t;
}
private int[] count(String str) {
int[] t = new int[26];
for(char c : str.toCharArray())
{
t[c - 'a']++;
}
return t;
}
}
}