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 :
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.