# CSPT13 Lecture 14 ## [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones) ``` class Solution: """ Understand jewels = "aA", stones = "aAAbbbb" output: 3 jewels = "" stones = "ab" output: 0 jewels = "a" stones = "aaa" output: 3 Plan jewels = "aA", stones = "aAAbbbb" j = {a, A} 1. make a set out of jewels 2. go through stones and count how many stones are jewels Runtime: O(S + J) Space: O(J) """ def numJewelsInStones(self, jewels: str, stones: str) -> int: j = set(jewels) numJewels = 0 for stone in stones: if stone in j: numJewels += 1 return numJewels ``` ## [Uncommon Words](https://leetcode.com/problems/uncommon-words-from-two-sentences) ``` from collections import defaultdict class Solution: """ Input: A = "this apple is sweet", B = "this apple is sour" Output: ["sweet","sour"] A = "apple apple" B = "banana" Output: ["banana"] A = "apple orange" B = "rambutan mango" Output: ["apple", "orange", "rambutan", "mango"] A = "apple apple" B = "mango mango" Output = [] Plan A = "apple apple" B = "banana" c = {apple: 2, banana: 1} res = ["banana"] Create one dictionary that contain the word count of both sentences Go through each word in both sentences If a word has count of 1 then that means it only occurs in one sentence add that to our result return result A = "apple apple" B = "banana" wordCount = {apple: 2, banana: 1} C = "apple apple banana" listC = [apple, apple, banana] res = [banana] Runtime: O(A + B) Space: O(A + B) """ def uncommonFromSentences(self, A: str, B: str) -> List[str]: wordCount = defaultdict(int) C = A + " " + B listC = C.split(" ") for word in listC: wordCount[word] += 1 res = [] for word in listC: if wordCount[word] == 1: res.append(word) return res ```