Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2d8d7a8

Browse files
add 1002
1 parent65745af commit2d8d7a8

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.ArrayList;
4+
importjava.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+
publicclass_1002 {
30+
publicstaticclassSolution1 {
31+
publicList<String>commonChars(String[]A) {
32+
int[][]charCount =newint[A.length][26];
33+
for (inti =0;i <A.length;i++) {
34+
for (charc :A[i].toCharArray()) {
35+
charCount[i][c -'a']++;
36+
}
37+
}
38+
List<String>result =newArrayList<>();
39+
for (inti =0;i <26;i++) {
40+
while (charCount[0][i] !=0) {
41+
charc = (char) (i +'a');
42+
booleanvalid =true;
43+
charCount[0][i]--;
44+
for (intj =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+
returnresult;
60+
}
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1002;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
publicclass_1002Test {
9+
privatestatic_1002.Solution1solution1;
10+
privatestaticString[]A;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1002.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
A =newString[] {"bella","label","roller"};
20+
CommonUtils.print(solution1.commonChars(A));
21+
}
22+
23+
@Test
24+
publicvoidtest2() {
25+
A =newString[] {"cool","lock","cook"};
26+
CommonUtils.print(solution1.commonChars(A));
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp