https://leetcode.com/problems/jewels-and-stones/description/
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".
Example 1:
Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
Example 2:
Input: jewels = "z", stones = "ZZ"
Output: 0
너무나도 기초적인 문제다.
1. C++
class Solution {
public:
int numJewelsInStones(string jewels, string stones) {
unordered_set<char> jewelSet(jewels.begin(), jewels.end());
int count = 0;
for (char stone : stones) {
if (jewelSet.find(stone) != jewelSet.end()) {
count++;
}
}
return count;
}
};
map을 선언하는 방법과, find 쓰는 거 정도만 유의하면 될듯
2. C
int numJewelsInStones(char* jewels, char* stones) {
int ans = 0;
int j_len = strlen(jewels);
int s_len = strlen(stones);
for(int i=0;i<s_len;i++){
for(int j=0;j<j_len;j++){
if(jewels[j] == stones[i]){
ans++;
break;
}
}
}
return ans;
}
3. Python
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
s = set()
ans = 0
for x in jewels:
s.add(x)
for y in stones:
if y in s :
ans +=1
return ans