-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjewelsAndStones.java
More file actions
37 lines (30 loc) · 1.32 KB
/
jewelsAndStones.java
File metadata and controls
37 lines (30 loc) · 1.32 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
36
37
/* 771. Jewels and Stones
* You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.
Letters are case sensitive, so "a" is considered a different type of stone from "A".
* Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
* Input: jewels = "z", stones = "ZZ"
Output: 0
All the characters of jewels are unique.
*/
public class jewelsAndStones {
public static int numJewelsInStones(String jewels, String stones){
int count = 0;
for(int i=0; i<jewels.length(); i++){
char charJewels = jewels.charAt(i);
for(int j=0; j<stones.length(); j++){
char charStones = stones.charAt(j);
if(charJewels == charStones){
count++;
}
}
}
return count;
}
public static void main(String[] args) {
String jewels = "aA";
String stones = "aAAbbbb";
System.out.println(numJewelsInStones(jewels, stones));
}
}
//Note: In case of primitive datatype, we can use '==' operator for checking the same value of left and right operators. In case of String, we need .equals() operator for checking the content.