Skip to content

Commit 2d8d7a8

Browse files
add 1002
1 parent 65745af commit 2d8d7a8

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1002.java) | O(n) | O(1) | |Easy|
3031
|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_999.java) | O(1) | O(1) | |Easy|
3132
|997|[Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_997.java) | O(n) | O(n) | |Easy|
3233
|994|[Rotting Oranges](https://leetcode.com/problems/rotting-oranges/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_994.java) | O(m*2*n*2) | O(m*n) | |Easy| BFS
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1002. Find Common Characters
8+
*
9+
* Given an array A of strings made only from lowercase letters,
10+
* return a list of all characters that show up in all strings within the list (including duplicates).
11+
* For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.
12+
*
13+
* You may return the answer in any order.
14+
*
15+
* Example 1:
16+
* Input: ["bella","label","roller"]
17+
* Output: ["e","l","l"]
18+
*
19+
* Example 2:
20+
* Input: ["cool","lock","cook"]
21+
* Output: ["c","o"]
22+
*
23+
* Note:
24+
*
25+
* 1 <= A.length <= 100
26+
* 1 <= A[i].length <= 100
27+
* A[i][j] is a lowercase letter
28+
*/
29+
public class _1002 {
30+
public static class Solution1 {
31+
public List<String> commonChars(String[] A) {
32+
int[][] charCount = new int[A.length][26];
33+
for (int i = 0; i < A.length; i++) {
34+
for (char c : A[i].toCharArray()) {
35+
charCount[i][c - 'a']++;
36+
}
37+
}
38+
List<String> result = new ArrayList<>();
39+
for (int i = 0; i < 26; i++) {
40+
while (charCount[0][i] != 0) {
41+
char c = (char) (i + 'a');
42+
boolean valid = true;
43+
charCount[0][i]--;
44+
for (int j = 1; j < A.length; j++) {
45+
if (charCount[j][i] == 0) {
46+
valid = false;
47+
break;
48+
} else {
49+
charCount[j][i]--;
50+
}
51+
}
52+
if (!valid) {
53+
break;
54+
} else {
55+
result.add("" + c);
56+
}
57+
}
58+
}
59+
return result;
60+
}
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1002;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _1002Test {
9+
private static _1002.Solution1 solution1;
10+
private static String[] A;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1002.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
A = new String[] {"bella", "label", "roller"};
20+
CommonUtils.print(solution1.commonChars(A));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
A = new String[] {"cool", "lock", "cook"};
26+
CommonUtils.print(solution1.commonChars(A));
27+
}
28+
}

0 commit comments

Comments
 (0)