Skip to content

Commit 83bc453

Browse files
add 771
1 parent 21effa7 commit 83bc453

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_771.java) | O(n) | O(m) | |Easy|
2526
|767|[Reorganize String](https://leetcode.com/problems/reorganize-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_767.java) | O(klogk) k is the number of unique characters in given String| O(k) | |Medium|
2627
|766|[Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_766.java) | O(m*n) | O(1) | |Easy|
2728
|765|[Couples Holding Hands](https://leetcode.com/problems/couples-holding-hands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_765.java) | O(n^2) | O(1) | |Hard|

Diff for: src/main/java/com/fishercoder/solutions/_771.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 771. Jewels and Stones
8+
9+
You're given strings J representing the types of stones that are jewels, and S representing the stones you have.
10+
Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.
11+
The letters in J are guaranteed distinct, and all characters in J and S are letters.
12+
Letters are case sensitive, so "a" is considered a different type of stone from "A".
13+
14+
Example 1:
15+
Input: J = "aA", S = "aAAbbbb"
16+
Output: 3
17+
18+
Example 2:
19+
Input: J = "z", S = "ZZ"
20+
Output: 0
21+
22+
Note:
23+
S and J will consist of letters and have length at most 50.
24+
The characters in J are distinct.
25+
*/
26+
27+
public class _771 {
28+
public static class Solution1 {
29+
public int numJewelsInStones(String J, String S) {
30+
Set<Character> set = new HashSet<>();
31+
for (char c : J.toCharArray()) {
32+
set.add(c);
33+
}
34+
int count = 0;
35+
for (char c : S.toCharArray()) {
36+
if (set.contains(c)) {
37+
count++;
38+
}
39+
}
40+
return count;
41+
}
42+
}
43+
}

Diff for: src/test/java/com/fishercoder/_771Test.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._771;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _771Test {
10+
private static _771.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _771.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.numJewelsInStones("aA", "aAAbbbb"));
20+
}
21+
}

0 commit comments

Comments
 (0)