Was it helpful?

Question

Program to count how many times we can find "pizza" with given string characters in Python

PythonServer Side ProgrammingProgramming

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

 Live Demo

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
raja
Published on 06-Oct-2020 09:56:22
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top