8/29 每日一題(檢查石頭中有多少珠寶,區分大小寫)
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 <= 50jewelsandstonesconsist of only English letters.- All the characters of
jewelsare unique.
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
res=0
j_list=list(jewels)
for i in j_list:
res+=stones.count(i)
return res
---------------
from collections import defaultdict
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
jew = set()
stCount = 0
for i in range(0, len(jewels)):
jew.add(jewels[i])
for i in range(0,len(stones)):
if stones[i] in jew:
stCount += 1
return stCount----------------class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
l=len(jewels)
l2=len(stones)
c=0
for i in range(0,l):
for j in range(0,l2):
if jewels[i]==stones[j]:
c+=1
return c
-----------------------------
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
d={}
for i in jewels:
if i in d:
d[i]+=1
else:
d[i]=1
ans=0
for j in stones:
if j in d:
ans+=d[j]
return ans
標籤: leetcode

0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁