[leetcode] 771. Jewels and Stones
2022. 12. 9. 09:20ㆍ노트/Algorithm : 알고리즘
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
Constraints:
- 1 <= jewels.length, stones.length <= 50
- jewels and stones consist of only English letters.
- All the characters of jewels are unique.
class Solution(object):
def numJewelsInStones(self, jewels, stones):
"""
:type jewels: str
:type stones: str
:rtype: int
"""
freqs = {}
for char in stones:
if char not in freqs:
freqs[char] = 1
else:
freqs[char] += 1
count = 0
for char in jewels:
if char in freqs:
count += freqs[char]
return count
class Solution(object):
def numJewelsInStones(self, jewels, stones):
"""
:type jewels: str
:type stones: str
:rtype: int
"""
freqs = collections.defaultdict(int)
count = 0
for char in stones:
freqs[char] += 1
for char in jewels:
count += freqs[char]
return count
class Solution(object):
def numJewelsInStones(self, jewels, stones):
"""
:type jewels: str
:type stones: str
:rtype: int
"""
freqs = collections.Counter(stones)
count = 0
for char in jewels:
count += freqs[char]
return count
class Solution(object):
def numJewelsInStones(self, jewels, stones):
"""
:type jewels: str
:type stones: str
:rtype: int
"""
return sum([s in jewels for s in stones])
'노트 > Algorithm : 알고리즘' 카테고리의 다른 글
[leetcode] 347. Top K Frequent Elements (0) | 2022.12.20 |
---|---|
[leetcode] 3. Longest Substring Without Repeating Characters (0) | 2022.12.19 |
[leetcode] 706. Design HashMap (1) | 2022.12.08 |
[leetcode] 23. Merge k Sorted Lists (0) | 2022.12.06 |
[leetcode] 641. Design Circular Deque (0) | 2022.12.06 |