Skip to content

Commit 147edd4

Browse files
solves jewels and stons
1 parent ffd3604 commit 147edd4

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@
205205
| 734 | [Sentence Similarity](https://leetcode.com/problems/sentence-similarity) | |
206206
| 744 | [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target) | [![Java](assets/java.png)](src/FindSmallestLetterGreaterThanTarget.java) [![Python](assets/python.png)](python/find_smallest_letter_greater_than.py) |
207207
| 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs) | [![Java](assets/java.png)](src/MinCostClimbingStairs.java) [![Python](assets/python.png)](python/min_cost_climbing_stairs.py) |
208-
| 747 | [Largest Number at least twize of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others) | [![Java](assets/java.png)](src/LargestNumberAtLeastTwiceOfOthers.java) [![Python](assets/python.png)](python/largest_number_at_least_twice_of_others.py) |
208+
| 747 | [Largest Number at least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others) | [![Java](assets/java.png)](src/LargestNumberAtLeastTwiceOfOthers.java) [![Python](assets/python.png)](python/largest_number_at_least_twice_of_others.py) |
209209
| 748 | [Shortest Completing Word](https://leetcode.com/problems/shortest-completing-word) | [![Java](assets/java.png)](src/ShortestCompletingWord.java) [![Python](assets/python.png)](python/shortest_completing_word.py) |
210210
| 758 | [Bold Words in String](https://leetcode.com/problems/bold-words-in-string) | |
211211
| 760 | [Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings) | |
212212
| 762 | [Prime Number of Set Bits in Primary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation) | [![Java](assets/java.png)](src/PrimeNumberOfSetBitsInBinaryRepresentation.java) [![Python](assets/python.png)](python/prime_number_of_set_bits_in_binary_representation.py) |
213213
| 766 | [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix) | [![Java](assets/java.png)](src/ToeplitzMatrix.java) [![Python](assets/python.png)](python/toeplitz_matrix.py) |
214-
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones) | |
214+
| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones) | [![Java](assets/java.png)](src/JewelsAndStones.java) [![Python](assets/python.png)](python/jewels_and_stones.py) |
215215
| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes) | [![Java](assets/java.png)](src/MinimumAbsoluteDifferenceInBST.java) [![Python](assets/python.png)](python/minimum_distance_between_bst_nodes.py) |
216216
| 788 | [Rotated Digits](https://leetcode.com/problems/rotated-digits) | |
217217
| 796 | [Rotate String](https://leetcode.com/problems/rotate-string) | |

python/jewels_and_stones.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def numJewelsInStones(self, jewels: str, stones: str) -> int:
3+
jewelTypes = set(jewels)
4+
result = 0
5+
for stone in stones:
6+
if stone in jewelTypes:
7+
result += 1
8+
return result

src/JewelsAndStones.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
public class JewelsAndStones {
5+
public int numJewelsInStones(String jewels, String stones) {
6+
Set<Character> jewelTypes = toSet(jewels);
7+
int totalJewels = 0;
8+
for (char stone : stones.toCharArray()) {
9+
if (jewelTypes.contains(stone)) {
10+
totalJewels++;
11+
}
12+
}
13+
return totalJewels;
14+
}
15+
16+
private Set<Character> toSet(String string) {
17+
Set<Character> set = new HashSet<>();
18+
for (char character : string.toCharArray()) {
19+
set.add(character);
20+
}
21+
return set;
22+
}
23+
}

0 commit comments

Comments
 (0)