Given a string S consisting of only characters ‘a’ and ‘b’, and an integer N. The string S is added N times to obtain string T. Your task is to count the number of prefixes where number of a is strictly greater than b.
string T = S + S + S + S ……. N times.
Input : aba 2 Output : 5 Explanation : The string T is "abaaba". It has five prefixes which contain more a-s than b-s: "a", "aba", "abaa", "abaab" and "abaaba". Input : baa 3 Output : 6 Explanation : The string T is "baabaabaa". The strings "baa", "baaba", "baabaa", "baabaab", "baabaaba" and "baabaabaa" are the six valid prefixes.
Naive approach : A simple way to do this program is to generate the entire string T and then run a loop checking for valid prefixes where number of a is greater than number of b. If value of N is very large, this method is not efficient and yet time consuming.
Efficient Approach :
Note that the string is repetitive. So, we do not have to check for the entire string T.
Operate on string S. Let,
count = Number of Prefixes in string S
A = Frequency of character ‘a’ in string S
B = Frequency of character ‘b’ in string S
CASE 1: count == 0
If number of valid prefixes is zero. Then, even if we generate the entire String T. Number of valid prefixes will still be zero.
CASE 2: count >0
This case has three sub-cases:
a. A == B
In this case, there is no affect of previous concatenations of S on incoming/new concatenation of S. In other words, when A != B, then there is some change in value of (A-B) after each addition of S to T, which affect the contribution of any future concatenation of S towards count.It means that since A == B, then number of b in T will not increase at same rate as number of a at each addition, which will affect contribution of next addition to the final answer. This is not the case when A == B . Hence, each addition of S will contribute count towards the final answer. There are N addition of S and we already found count by simple looping earlier. Hence, for this case, Answer = count * N.
b. A < B
In this case, because A < B, each new addition of S to T will decrease A-B .In other words, the number of b in T will increase more quickly than number of a, which will reduce the contribution of every future addition of S towards final answer. We see that, after every addition, the contribution of next addition must reduce by atleast 1. So gradually the count per new string will converge to zero. So we have to check until that happens.
For example, Say count of string S converges to zero after 1000 addition. If N = 99999, we just have to check till 1000 and ignore rest of the cases. If N = 5, we have to calculate till 5 additions.
c. A > B
Clearly, each new addition of S to T will increase A-B. Thus, the number of a in T will increase more quickly than number of b, which will increase the contribution of every future addition of S towards final answer. The maximum possible contribution of an addition to our answer can be |S|, i.e. the length of string S. So the count per string will saturate to length of the string after some additions. We have to check until that happens.
For example : Say count of string S saturates to length of S after X additions. So, we have to calculate count till X, then add the residue which is equal to (N-X)*length of S (iff N>X)
if N<X then we have to calculate till N additions.
Below is the implementation of above approach :
- Shift all prefixes by given lengths
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Count all prefixes of the given binary array which are divisible by x
- Python | Count all prefixes in given string with greatest frequency
- Check whether two strings can be made equal by increasing prefixes
- Queries to find the count of characters preceding the given location
- Number of ways N can be divided into four parts to construct a rectangle
- Find the count of natural Hexadecimal numbers of size N
- Largest Even and Odd N-digit numbers in Octal Number System
- Number of K length subsequences with minimum sum
- Check whether the given integers a, b, c and d are in proportion
- Count of matchsticks required to represent the given number
- Count number of ways to arrange first N numbers
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.