Was it helpful?

Question

Program to find number of string we can make where 'a' can be 'a' or 'b', and 'b' remains 'b'in Python

PythonServer Side ProgrammingProgramming

Suppose we have a string s with only "a" and "b". "a"s can stay "a" or turn into "b", but "b"s can not be changed. We have to find the number of unique strings that we can make.

So, if the input is like s = "baab", then the output will be 4, as We can make these strings − ["baab", "babb", "bbab", "bbbb"]

To solve this, we will follow these steps −

  • counts := frequency of 'a' in s
  • return 2^counts

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, s):
      counts = s.count('a')
      total = 2**(counts)
      return total
ob = Solution()
print(ob.solve("baab"))

Input

"baab"

Output

4
raja
Published on 05-Oct-2020 11:20:07
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top