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".
jewels and stones consist of only English letters.
All the characters of jewels are unique.
Solutions
Solution 1: Hash Table or Array
Thinking
Count how many stones are jewels. Length \(50\): put jewels in a set and test each stone. Case matters.
We can first use a hash table or array \(s\) to record all types of jewels. Then traverse all the stones, and if the current stone is a jewel, increment the answer by one.
The time complexity is \(O(m+n)\), and the space complexity is \(O(|\Sigma|)\), where \(m\) and \(n\) are the lengths of the strings \(\textit{jewels}\) and \(\textit{stones}\) respectively, and \(\Sigma\) is the character set, which in this problem is the set of all uppercase and lowercase English letters.