# Prefixes with more a than b

• Difficulty Level : Medium
• Last Updated : 07 Feb, 2023

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.

Examples :

```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 a 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:

1. A == B
• In this case, there is no effect of previous concatenations of S on incoming/new concatenation of S. In other words, when A != B, then there is some change in the 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 an at each addition, which will affect the 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.
2. 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 toward the 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.
3.  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 (if N>X)
if N<X then we have to calculate till N additions.

Below is the implementation of above approach:

## C++

 `// CPP code to count the prefixes``// with more a than b``#include ` `using` `namespace` `std;` `// Function to count prefixes``int` `prefix(string k, ``int` `n)``{``    ``int` `a = 0, b = 0, count = 0;``    ``int` `i = 0;``    ``int` `len = k.size();` `    ``// calculating for string S``    ``for` `(i = 0; i < len; i++) {``        ``if` `(k[i] == ``'a'``)``            ``a++;` `        ``if` `(k[i] == ``'b'``)``            ``b++;` `        ``if` `(a > b) {``            ``count++;``        ``}``    ``}` `    ``// count==0 or when N==1``    ``if` `(count == 0 || n == 1) {``        ``cout << count << endl;``        ``return` `0;``    ``}` `    ``// when all characters are a or a-b==0``    ``if` `(count == len || a - b == 0) {``        ``cout << count * n << endl;``        ``return` `0;``    ``}` `    ``int` `n2 = n - 1, count2 = 0;` `    ``// checking for saturation of``    ``// string after repetitive addition``    ``while` `(n2 != 0) {``        ``for` `(i = 0; i < len; i++) {``            ``if` `(k[i] == ``'a'``)``                ``a++;` `            ``if` `(k[i] == ``'b'``)``                ``b++;` `            ``if` `(a > b) {``                ``count2++;``            ``}``        ``}` `        ``count += count2;``        ``n2--;` `        ``if` `(count2 == 0)``            ``break``;` `        ``if` `(count2 == len) {``            ``count += (n2 * count2);``            ``break``;``        ``}` `        ``count2 = 0;``    ``}` `    ``return` `count;``}` `// Driver function``int` `main()``{``    ``string S = ``"aba"``;``    ``int` `N = 2;``    ``cout << prefix(S, N) << endl;` `    ``S = ``"baa"``;``    ``N = 3;``    ``cout << prefix(S, N) << endl;` `    ``return` `0;``}`

## Java

 `// Java code to count the``// prefixes with more a than b``import` `java.io.*;` `class` `GFG``{` `// Function to``// count prefixes``static` `int` `prefix(String k, ``int` `n)``{``    ``int` `a = ``0``, b = ``0``,``               ``count = ``0``;``    ``int` `i = ``0``;``    ``int` `len = k.length();` `    ``// calculating for string S``    ``for` `(i = ``0``; i < len; i++)``    ``{``        ``if` `(k.charAt(i) == ``'a'``)``            ``a++;` `        ``if` `(k.charAt(i) == ``'b'``)``            ``b++;` `        ``if` `(a > b)``        ``{``            ``count++;``        ``}``    ``}` `    ``// count==0 or when N==1``    ``if` `(count == ``0` `|| n == ``1``)``    ``{``        ``System.out.println(count);``        ``return` `0``;``    ``}` `    ``// when all characters``    ``// are a or a-b==0``    ``if` `(count == len || a - b == ``0``)``    ``{``        ``System.out.println(count * n);``        ``return` `0``;``    ``}` `    ``int` `n2 = n - ``1``, count2 = ``0``;` `    ``// checking for saturation``    ``// of string after repetitive``    ``// addition``    ``while` `(n2 != ``0``)``    ``{``        ``for` `(i = ``0``; i < len; i++)``        ``{``            ``if` `(k.charAt(i) == ``'a'``)``                ``a++;` `            ``if` `(k.charAt(i) == ``'b'``)``                ``b++;` `            ``if` `(a > b)``            ``{``                ``count2++;``            ``}``        ``}` `        ``count += count2;``        ``n2--;` `        ``if` `(count2 == ``0``)``            ``break``;` `        ``if` `(count2 == len)``        ``{``            ``count += (n2 * count2);``            ``break``;``        ``}` `        ``count2 = ``0``;``    ``}` `    ``return` `count;``}` `// Driver Code``public` `static` `void` `main (String[] args)``{``    ``String S = ``"aba"``;``    ``int` `N = ``2``;``    ``System.out.println(prefix(S, N));``    ` `    ``S = ``"baa"``;``    ``N = ``3``;``    ``System.out.println(prefix(S, N)) ;``}``}` `// This code is contributed``// by anuj_67.`

## Python3

 `# Python3 code to count the prefixes``# with more a than b` `# Function to count prefixes``def` `prefix(k, n):` `    ``a ``=` `0``    ``b ``=` `0``    ``count ``=` `0``    ``i ``=` `0``    ``Len` `=` `len``(k)` `    ``# calculating for string S``    ``for` `i ``in` `range``(``Len``):``        ``if` `(k[i] ``=``=` `"a"``):``            ``a ``+``=` `1` `        ``if` `(k[i] ``=``=` `"b"``):``            ``b ``+``=` `1` `        ``if` `(a > b) :``            ``count ``+``=` `1``        ` `    ``# count==0 or when N==1``    ``if` `(count ``=``=` `0` `or` `n ``=``=` `1``):``        ``print``(count)``        ``return` `0` `    ``# when all characters are a or a-b==0``    ``if` `(count ``=``=` `Len` `or` `a ``-` `b ``=``=` `0``) :``        ``print``(count ``*` `n)``        ``return` `0` `    ``n2 ``=` `n ``-` `1``    ``count2 ``=` `0` `    ``# checking for saturation of``    ``# string after repetitive addition``    ``while` `(n2 !``=` `0``):``        ``for` `i ``in` `range``(``Len``):``            ``if` `(k[i] ``=``=` `"a"``):``                ``a ``+``=` `1` `            ``if` `(k[i] ``=``=` `"b"``):``                ``b ``+``=` `1` `            ``if` `(a > b):``                ``count2 ``+``=` `1``            ` `        ``count ``+``=` `count2``        ``n2 ``-``=` `1` `        ``if` `(count2 ``=``=` `0``):``            ``break` `        ``if` `(count2 ``=``=` `Len``):``            ``count ``+``=` `(n2 ``*` `count2)``            ``break``        ` `        ``count2 ``=` `0``    ` `    ``return` `count` `# Driver Code``S ``=` `"aba"``N ``=` `2``print``(prefix(S, N))` `S ``=` `"baa"``N ``=` `3``print``(prefix(S, N))` `# This code is contributed by``# Mohit kumar 29`

## C#

 `// C# code to count the``// prefixes with more``// a than b``using` `System;` `class` `GFG``{` `// Function to``// count prefixes``static` `int` `prefix(String k, ``int` `n)``{``    ``int` `a = 0, b = 0,``        ``count = 0;``    ``int` `i = 0;``    ``int` `len = k.Length;` `    ``// calculating for string S``    ``for` `(i = 0; i < len; i++)``    ``{``        ``if` `(k[i] == ``'a'``)``            ``a++;` `        ``if` `(k[i] == ``'b'``)``            ``b++;` `        ``if` `(a > b)``        ``{``            ``count++;``        ``}``    ``}` `    ``// count==0 or when N==1``    ``if` `(count == 0 || n == 1)``    ``{``        ``Console.WriteLine(count);``        ``return` `0;``    ``}` `    ``// when all characters``    ``// are a or a-b==0``    ``if` `(count == len ||``        ``a - b == 0)``    ``{``        ``Console.WriteLine(count * n);``        ``return` `0;``    ``}` `    ``int` `n2 = n - 1, count2 = 0;` `    ``// checking for saturation``    ``// of string after repetitive``    ``// addition``    ``while` `(n2 != 0)``    ``{``        ``for` `(i = 0; i < len; i++)``        ``{``            ``if` `(k[i] == ``'a'``)``                ``a++;` `            ``if` `(k[i] == ``'b'``)``                ``b++;` `            ``if` `(a > b)``            ``{``                ``count2++;``            ``}``        ``}` `        ``count += count2;``        ``n2--;` `        ``if` `(count2 == 0)``            ``break``;` `        ``if` `(count2 == len)``        ``{``            ``count += (n2 * count2);``            ``break``;``        ``}` `        ``count2 = 0;``    ``}` `    ``return` `count;``}` `// Driver Code``public` `static` `void` `Main ()``{``    ``string` `S = ``"aba"``;``    ``int` `N = 2;``    ``Console.WriteLine(prefix(S, N));``    ` `    ``S = ``"baa"``;``    ``N = 3;``    ``Console.WriteLine(prefix(S, N)) ;``}``}` `// This code is contributed``// by anuj_67.`

## PHP

 ` ``\$b``)``        ``{``            ``\$count``++;``        ``}``    ``}` `    ``// count==0 or when N==1``    ``if` `(``\$count` `== 0 || ``\$n` `== 1)``    ``{``        ``echo``(``\$count``);``        ``return` `0;``    ``}` `    ``// when all characters``    ``// are a or a-b==0``    ``if` `(``\$count` `== ``\$len` `|| ``\$a` `- ``\$b` `== 0)``    ``{``        ``echo``(``\$count` `* ``\$n``);``        ``return` `0;``    ``}` `    ``\$n2` `= ``\$n` `- 1; ``\$count2` `= 0;` `    ``// checking for saturation``    ``// of string after repetitive``    ``// addition``    ``while` `(``\$n2` `!= 0)``    ``{``        ``for` `(``\$i` `= 0; ``\$i` `< ``\$len``; ``\$i``++)``        ``{``            ``if` `(``\$k``[``\$i``] == ``'a'``)``                ``\$a``++;` `            ``if` `(``\$k``[``\$i``] == ``'b'``)``                ``\$b``++;` `            ``if` `(``\$a` `> ``\$b``)``            ``{``                ``\$count2``++;``            ``}``        ``}` `        ``\$count` `+= ``\$count2``;``        ``\$n2``--;` `        ``if` `(``\$count2` `== 0)``            ``break``;` `        ``if` `(``\$count2` `== ``\$len``)``        ``{``            ``\$count` `+= (``\$n2` `* ``\$count2``);``            ``break``;``        ``}` `        ``\$count2` `= 0;``    ``}` `    ``return` `\$count``;``}` `// Driver Code``\$S` `= ``"aba"``;``\$N` `= 2;``echo``(prefix(``\$S``,``\$N``).``"\n"``);` `\$S` `= ``"baa"``;``\$N` `= 3;``echo``(prefix(``\$S``, ``\$N``).``"\n"``);` `// This code is contributed``// by Mukul Singh.`

## Javascript

 ``

Output

```5
6```

Time Complexity: O(L * N), where L is the length of the given string and N is the given input.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up