[New Solution] - "Jewels and Stones"

This commit is contained in:
Hao Chen 2018-06-23 15:37:23 +08:00
parent 79ee9680db
commit db71bb2ada
2 changed files with 50 additions and 0 deletions

View File

@ -9,6 +9,7 @@ LeetCode
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|837|[Most Common Word](https://leetcode.com/problems/most-common-word/) | [C++](./algorithms/cpp/mostCommonWord/MostCommonWord.cpp)|Easy|
|782|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description) | [C++](./algorithms/cpp/jewelsAndStones/JewelsAndStones.cpp)|Easy|
|643|[Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/description/) | [C++](./algorithms/cpp/maximumAverageSubarray/MaximumAverageSubarray.I.cpp)|Easy|
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [C++](./algorithms/cpp/totalHammingDistance/totalHammingDistance.cpp)|Medium|
|418|[SentenceScreenFitting](https://leetcode.com/problems/sentence-screen-fitting/) ♥ | [C++](./algorithms/cpp/sentenceScreenFitting/sentenceScreenFitting.cpp)|Easy|

View File

@ -0,0 +1,49 @@
// Source : https://leetcode.com/problems/jewels-and-stones/description
// Author : Hao Chen
// Date : 2018-06-23
/***************************************************************************************
*
* 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.
***************************************************************************************/
class Solution {
public:
int numJewelsInStones(string J, string S) {
bool map[256] = {false};
for (auto c : J) {
map[c]=true;
}
int cnt=0;
for (auto c : S) {
if (map[c]) cnt++;
}
return cnt;
}
};