-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJewelsAndStones.java
More file actions
28 lines (24 loc) · 872 Bytes
/
JewelsAndStones.java
File metadata and controls
28 lines (24 loc) · 872 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
package solutions;
// [Problem] https://leetcode.com/problems/jewels-and-stones
class JewelsAndStones {
// O(n) time, O(1) space
public int numJewelsInStones(String jewels, String stones) {
int[] jewelCounts = new int[58];
int totalCount = 0;
for (char jewel : jewels.toCharArray()) {
jewelCounts[jewel - 'A'] = 1;
}
for (char stone : stones.toCharArray()) {
totalCount += jewelCounts[stone - 'A'];
}
return totalCount;
}
// test
public static void main(String[] args) {
JewelsAndStones solution = new JewelsAndStones();
String jewels = "a", stones = "aAAbbbb";
int expectedOutput = 3;
int actualOutput = solution.numJewelsInStones(jewels, stones);
System.out.println("Test passed? " + (expectedOutput == actualOutput));
}
}