## Saturday, May 5, 2018

### LeetCode 771 - Jewels and Stones

https://leetcode.com/problems/jewels-and-stones/description/
You're given strings `J` representing the types of stones that are jewels, and `S` representing the stones you have.  Each character in `S`is a type of stone you have.  You want to know how many of the stones you have are also jewels.
The letters in `J` are guaranteed distinct, and all characters in `J` and `S` are letters. Letters are case sensitive, so `"a"` is considered a different type of stone from `"A"`.
Example 1:
```Input: J = "aA", S = "aAAbbbb"
Output: 3
```
Example 2:
```Input: J = "z", S = "ZZ"
Output: 0
```
Note:
• `S` and `J` will consist of letters and have length at most 50.
• The characters in `J` are distinct.
https://leetcode.com/problems/jewels-and-stones/discuss/113553/C++JavaPython-Easy-and-Concise-Solution-O(M+N)

``````public int numJewelsInStones(String J, String S) {
int res = 0;
Set setJ = new HashSet();
for (char s: S.toCharArray()) if (setJ.contains(s)) res++;
return res;
}``````
https://leetcode.com/problems/jewels-and-stones/discuss/113574/1-liners-PythonJavaRuby
``````public int numJewelsInStones(String J, String S) {
return S.replaceAll("[^" + J + "]", "").length();
}``````
https://leetcode.com/problems/jewels-and-stones/discuss/113562/Java-solution
``````int res = 0, index = 0;
for (int i = 0; i < J.length(); i++) {
char jewel = J.charAt(i);
while ((index = S.indexOf(String.valueOf(jewel), index)) != -1) {
++res;
++index;
}
}
return res;``````