Skip to content

Commit 540e510

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.6 MB (53.24%)
1 parent a1a3394 commit 540e510

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

0782-jewels-and-stones/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<p>You&#39;re given strings <code>jewels</code> representing the types of stones that are jewels, and <code>stones</code> representing the stones you have. Each character in <code>stones</code> is a type of stone you have. You want to know how many of the stones you have are also jewels.</p>
2+
3+
<p>Letters are case sensitive, so <code>&quot;a&quot;</code> is considered a different type of stone from <code>&quot;A&quot;</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<pre><strong>Input:</strong> jewels = "aA", stones = "aAAbbbb"
8+
<strong>Output:</strong> 3
9+
</pre><p><strong class="example">Example 2:</strong></p>
10+
<pre><strong>Input:</strong> jewels = "z", stones = "ZZ"
11+
<strong>Output:</strong> 0
12+
</pre>
13+
<p>&nbsp;</p>
14+
<p><strong>Constraints:</strong></p>
15+
16+
<ul>
17+
<li><code>1 &lt;=&nbsp;jewels.length, stones.length &lt;= 50</code></li>
18+
<li><code>jewels</code> and <code>stones</code> consist of only English letters.</li>
19+
<li>All the characters of&nbsp;<code>jewels</code> are <strong>unique</strong>.</li>
20+
</ul>

0782-jewels-and-stones/solution.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Approach 2: Hash Set
2+
3+
# m = len(jewels), n = len(stones)
4+
# Time: O(m + n)
5+
# Space: O(m)
6+
7+
class Solution:
8+
def numJewelsInStones(self, jewels: str, stones: str) -> int:
9+
Jset = set(jewels)
10+
11+
return sum(s in Jset for s in stones)
12+

0 commit comments

Comments
 (0)