
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Jewels and Stones in Python
Suppose we have a string J that indicates some letters that are considered as Jewel, and another string S, that represents some stones that we have. Our task is to find how many of stones in S is also jewel. The letters in J and S are case sensitive. So if the J = “aZc”, and S = “catTableZebraPicnic” then there are 7 jewels.
To solve this we will convert the string into a list of characters. If the character in J present in S, then increase the count.
Example
Let us see the following implementation to get better understanding −
class Solution(object): def numJewelsInStones(self, J, S): jewels = {} for i in J: jewels[i] = 1 number = 0 for i in S: if i in jewels: number+=1 return number ob1 = Solution() print(ob1.numJewelsInStones("aZc", "catTableZebraPicnic"))
Input
"aZc" "catTableZebraPicnic"
Output
7
- Related Articles
- Program to find maximum score from removing stones in Python
- Program to find minimum cost to merge stones in Python
- Difference Between Gallstones and Kidney Stones
- How to Convert Kilograms to Stones and Pounds in Excel?
- Program to check we can cross river by stones or not in Python
- Moving Stones Until Consecutive II in C++
- Minimum Cost to Merge Stones in C++
- Tonsil Stones Treatment: Home Remedies, Surgery
- Pain Remains After Passing Kidney Stones
- Why You Keep Getting Tonsil Stones and How to Prevent Them
- What causes the sailing stones to move?
- Two stones are thrown vertically upwards simultaneously with their initial velocities u1 and u2respectively. Find the ratio of the height reached by the two stones.
- C++ code to find minimum stones after all operations
- A rectangular courtyard is 20m 16 cm long and 15m 60 cm broad. It is to be paved with square stones of the same size. Find the least possible number of such stones.
- C++ code to count number of times stones can be given

Advertisements