-
Notifications
You must be signed in to change notification settings - Fork 1
/
FindCommonCharacters.java
56 lines (50 loc) · 1.6 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
51
52
53
54
55
56
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// https://leetcode.com/problems/find-common-characters/
public class FindCommonCharacters {
private final String[] A;
public FindCommonCharacters(String[] input) {
this.A = input;
}
public List<String> solution() {
if (A.length == 1) {
return Collections.emptyList();
}
int[] common = new int[128];
String firstString = A[0];
for (int i = 0; i < firstString.length(); i++) {
char c = firstString.charAt(i);
if (common[c] != 0) {
common[c] = common[c] + 1;
} else {
common[c] = 1;
}
}
for (int i = 1; i < A.length; i++) {
int[] letters = new int[128];
String word = A[i];
for (int j = 0; j < word.length(); j++) {
char c = word.charAt(j);
if (letters[c] != 0) {
letters[c] = letters[c] + 1;
} else {
letters[c] = 1;
}
}
for (int j = 0; j < common.length; j++) {
common[j] = Math.min(common[j], letters[j]);
}
}
List<String> result = new ArrayList<>();
for (int i = 0; i < common.length; i++) {
if (common[i] != 0) {
for (int j = 0; j < common[i]; j++) {
result.add(String.valueOf((char) i));
}
}
}
return result;
}
}