
- 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
Counting Number of Dinosaurs in Python
Suppose we have a string called animals and another string called dinosaurs. Every letter in animals represents a different type of animal and every unique character in dinosaurs string represents a different dinosaur. We have to find the total number of dinosaurs in animals.
So, if the input is like animals = "xyxzxyZ" dinosaurs = "yZ", then the output will be 3, as there are two types of dinosaurs y and Z, in the animal string there are two y type animal and one Z type animal.
To solve this, we will follow these steps −
- res := 0
- dinosaurs := a new set by taking elements from dinosaurs
- for each c in dinosaurs, do
- res := res + occurrence of c in animals
- return res
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, animals, dinosaurs): res = 0 dinosaurs = set(dinosaurs) for c in dinosaurs: res += animals.count(c) return res ob = Solution() animals = "xyxzxyZ" dinosaurs = "yZ" print(ob.solve(animals, dinosaurs))
Input
"xyxzxyZ", "yZ"
Output
3
- Related Articles
- Counting number of 9s encountered while counting up to n in JavaScript
- Number of letters in the counting JavaScript
- Counting Bits in Python
- Counting number of words in a sentence in JavaScript
- Counting divisors of a number using JavaScript
- Counting number of characters in text file using java
- Counting number of positive and negative votes in MySQL?
- Counting the number of 1s upto n in JavaScript
- Counting number of vowels in a string with JavaScript
- Counting number of repeating words in a Golang String
- Counting number of triangle sides in an array in JavaScript
- Why dinosaurs are known as the ancestors of birds?
- Counting number of paragraphs in text file using java\n
- Counting number of lines in text file using java\n
- Counting the number of redundant characters in a string - JavaScript

Advertisements