-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path771.java
More file actions
35 lines (35 loc) · 1.17 KB
/
771.java
File metadata and controls
35 lines (35 loc) · 1.17 KB
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
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int numJewelsInStones(String J, String S) {
int[] arr = new int[128];
for (char ch: S.toCharArray()) {
arr[ch] += 1;
}
int res = 0;
for (char ch: J.toCharArray()) {
res += arr[ch];
}
return res;
}
}
__________________________________________________________________________________________________
sample 34396 kb submission
class Solution {
public int numJewelsInStones(String J, String S) {
Map<Character, Integer> stonesByType = new HashMap<>();
for (Character c : S.toCharArray()) {
if (J.indexOf(c) != -1) {
stonesByType.merge(c, 1, (oldValue, newValue) -> ++oldValue);
}
}
int result = 0;
for (char c : J.toCharArray()) {
if (stonesByType.containsKey(c)) {
result += stonesByType.get(c);
}
}
return result;
}
}
__________________________________________________________________________________________________