- Trending Categories
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 find number of string we can make where 'a' can be 'a' or 'b', and 'b' remains 'b'in Python
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
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
Advertisements