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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Shift all prefixes by given lengths
- Find K for every Array element such that at least K prefixes are ≥ K
- 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 XOR of an Array after replacing all occurrences of X by Y
- Minimum Sum of a pair at least K distance apart from an Array
- Maximize length of the String by concatenating characters from an Array of Strings
- Count total set bits in all numbers from range L to R
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Program to insert dashes between two adjacent odd digits in given Number
- Program to find Greatest Common Divisor (GCD) of N strings
- Product of absolute difference of every pair in given Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.