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