-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindCommonCharacters.java
50 lines (43 loc) · 1.35 KB
/
FindCommonCharacters.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
40
41
42
43
44
45
46
47
48
49
50
package string;
import utils.LeetCode;
import utils.Level;
import java.util.ArrayList;
import java.util.List;
@LeetCode(
no=1002,title = "Find Common Characters" ,level = Level.EASY,url = "https://leetcode.com/problems/find-common-characters/"
)
public class FindCommonCharacters {
public static void main(String[] args) {
FindCommonCharacters f= new FindCommonCharacters();
System.out.println(f.commonChars(new String[]{"bella","label","roller"}));
}
public List<String> commonChars(String[] A) {
List<String> result= new ArrayList<>();
int prev[]= new int[26];
for(int i=0;i<A.length;i++){
String s=A[i];
int []current=new int[26];
for(int j=0;j<s.length();j++){
char c=s.charAt(j);
if(i==0){
prev[c-'a']++ ;
}
current[c-'a']++;
}
if(i>0){
for(int j=0;j<current.length;j++){
prev[j]=Math.min(prev[j],current[j]);
}
}
}
for(int j=0;j<prev.length;j++){
if(prev[j]>0) {
int n=prev[j];
for(int k=0;k< n;k++) {
result.add(String.valueOf( (char) (j+'a')));
}
}
}
return result;
}
}