- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to count how many times we can find "pizza" with given string characters in Python
Suppose we have a lowercase string s, we have to find how many "pizza" strings we can make using the characters present in s. We can use the characters in s in any order, but each character can be used once.
So, if the input is like "ihzapezlzzilaop", then the output will be 2.
To solve this, we will follow these steps −
- p_freq := Frequency of 'p' in s
- i_freq := Frequency of 'i' in s
- z_freq := Frequency of 'z' in s
- a_freq := Frequency of 'a' in s
- return minimum of (p_freq, i_freq, z_freq/2 and a_freq)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, s): p_freq = s.count('p') i_freq = s.count('i') z_freq = s.count('z') a_freq = s.count('a') return min(p_freq, i_freq, z_freq // 2, a_freq) ob = Solution() print(ob.solve("ihzapezlzzilaop"))
Input
"ihzapezlzzilaop"
Output
2
Advertisements