# LeetCode 711 ###### tags: `python`,`LeetCode` >這邊使用Python解題 ## 題目: 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". ## 範例 ### 範例 1: ``` Input: jewels = "aA", stones = "aAAbbbb" Output: 3 ``` ### 範例 2: ``` Input: jewels = "z", stones = "ZZ" Output: 0 ``` ## 條件限制 * 1 <= jewels.length, stones.length <= 50 * jewels and stones consist of only English letters. * All the characters of jewels are unique. ## 我的解題思路: 因為我這邊使用的是 Python,那就可以很簡單的把字串格式轉換成List,利用 For Loop 去做存取 ## 程式碼: ``` def numJewelsInStones(jewels, stones): result = 0 jewels = list(jewels) stones = list(stones) for i in stones: if i in jewels: result+=1 return result ``` ## 測試 ``` test_input1 = ["aA","z","cC"] test_input2 = ["aAAbbbb","ZZ","ccCCCooo"] test_output = [3,0,5] for idx in range(len(test_input1)): result = None try: result = numJewelsInStones(test_input1[idx],test_input2[idx]) except Exception as ex: print(ex) if result == test_output[idx]: print('case ',idx,' pass') else: print('case ',idx,': failed. your ans is', result, 'expect', test_output[idx]) ```