forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumJeweIsInStones.java
More file actions
37 lines (34 loc) · 1020 Bytes
/
NumJeweIsInStones.java
File metadata and controls
37 lines (34 loc) · 1020 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
36
37
package str;
import java.util.HashSet;
import java.util.Set;
public class NumJeweIsInStones {
public int numJeweIsInStones(String J,String S){
// Map<Character,Integer> map = new HashMap<>();
// int r = 0;
// for (int i = 0; i < J.length(); i++) {
// char j = J.charAt(i);
// map.put(j,i);
// }
// for (int i = 0; i < S.length(); i++) {
// char s = S.charAt(i);
// if (map.containsKey(s)) r++;
// }
// return r;
int r = 0;
Set set = new HashSet<>();
for (char j : J.toCharArray()){
set.add(j);
}
for (char s : S.toCharArray()){
if (set.contains(s)) r++;
}
return r;
}
public static void main(String[] args) {
String J = "aA";
String S = "aAAbbbb";
NumJeweIsInStones numJeweIsInStones = new NumJeweIsInStones();
int r = numJeweIsInStones.numJeweIsInStones(J,S);
System.out.println(r);
}
}