Leetcode / 771. Jewels and Stones
Pick a programming language:
Here is the source code for the solution to this problem.
class Solution {
// time O(n)
// space O(n)
public int numJewelsInStones(String jewels, String stones) {
HashSet<Character> hashSet = new HashSet<Character>();
int count = 0;
for (int i = 0; i < jewels.length(); i++) {
hashSet.add(jewels.charAt(i));
}
for (int i = 0; i < stones.length(); i++) {
if (hashSet.contains(stones.charAt(i))) {
count++;
}
}
return count;
}
}
public class Solution {
public int NumJewelsInStones(string jewels, string stones) {
HashSet<char> hashSet = new HashSet<char>();
foreach (char c in jewels) {
hashSet.Add(c);
}
int count = 0;
foreach (char c in stones) {
if (hashSet.Contains(c)) {
count++;
}
}
return count;
}
}
Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: