The Jewels and Stones interview question is a classic string counting problem. You are given two strings: jewels, where each character represents a type of stone that is a jewel, and stones, representing the stones you have. Your goal is to count how many of your stones are also jewels. Note that characters are case-sensitive.
This is one of the most common introductory questions at Amazon, Apple, and Google. It tests your knowledge of Hash Table interview patterns and the basic time complexity of different data structures. It evaluations if you know that searching in a Set is , while searching in a string or array is .
This problem follows the Hash Set for Membership pattern.
jewels string into a Hash Set of characters.stones string character by character.jewels set.jewels = "aA", stones = "aAAbbbb"
jewelSet = {'a', 'A'}.stones:
Even for "Easy" problems, mention the Time and Space Complexity. Time is where is number of jewels and is number of stones. Space is to store the jewels. This demonstrates a professional approach to even simple tasks.