-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJewelsAndStones.java
More file actions
36 lines (26 loc) · 834 Bytes
/
Copy pathJewelsAndStones.java
File metadata and controls
36 lines (26 loc) · 834 Bytes
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
package LeetcodePractice;
import java.util.HashSet;
import java.util.Set;
public class JewelsAndStones {
public static int numJewelsInStones(String jewels, String stones) {
Set<Character> jewelSet = new HashSet<>();
for (char c : jewels.toCharArray()) {
jewelSet.add(c);
}
int count = 0;
for (char s : stones.toCharArray()) {
if (jewelSet.contains(s)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
String jewels1 = "aA";
String stones1 = "aAAbbbb";
System.out.println(numJewelsInStones(jewels1, stones1));
String jewels2 = "z";
String stones2 = "ZZ";
System.out.println(numJewelsInStones(jewels2, stones2));
}
}