-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJewelsAndStones.java
More file actions
33 lines (25 loc) · 844 Bytes
/
JewelsAndStones.java
File metadata and controls
33 lines (25 loc) · 844 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
import java.util.*;
public class JewelsAndStones {
public int numJewelsInStones(String jewels, String stones) {
// HashSet<Character> ch = new HashSet<>();
// for (char c : jewels.toCharArray()) {
// ch.add(c);
// }
// int count = 0;
// for (char s : stones.toCharArray()) {
// if (ch.contains(s) == true) {
// count++;
// }
// }
// return count;
int count = 0;
HashSet<Character> jew =new HashSet<>();
for(int i = 0; i< jewels.length();i++) jew.add(jewels.charAt(i));
for(int i = 0; i< stones.length();i++) {
char c= stones.charAt(i);
if(jew.contains(c))
count++;
}
return count;
}
}