forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
39 lines (29 loc) · 787 Bytes
/
main2.cpp
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
38
39
/// Source : https://leetcode.com/problems/jewels-and-stones/description/
/// Author : liuyubobobo
/// Time : 2018-01-27
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
using namespace std;
/// Using Hash Set
/// Time Complexity: O(len(J) + len(S))
/// Space Complxity: O(len(J))
class Solution {
public:
int numJewelsInStones(string J, string S) {
unordered_set<char> jewels;
for(char c: J)
jewels.insert(c);
int res = 0;
for(char c: S)
if(jewels.find(c) != jewels.end())
res ++;
return res;
}
};
int main() {
cout << Solution().numJewelsInStones("aA", "aAAbbbb") << endl;
cout << Solution().numJewelsInStones("z", "ZZ") << endl;
return 0;
}