Prefixes with more a than b
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:
- 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.
- 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
int prefix(string k, int n)
{
int a = 0, b = 0, count = 0;
int i = 0;
int len = k.size();
for (i = 0; i < len; i++) {
if (k[i] == 'a' )
a++;
if (k[i] == 'b' )
b++;
if (a > b) {
count++;
}
}
if (count == 0 || n == 1) {
cout << count << endl;
return 0;
}
if (count == len || a - b == 0) {
cout << count * n << endl;
return 0;
}
int n2 = n - 1, count2 = 0;
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;
}
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
import java.io.*;
class GFG
{
static int prefix(String k, int n)
{
int a = 0 , b = 0 ,
count = 0 ;
int i = 0 ;
int len = k.length();
for (i = 0 ; i < len; i++)
{
if (k.charAt(i) == 'a' )
a++;
if (k.charAt(i) == 'b' )
b++;
if (a > b)
{
count++;
}
}
if (count == 0 || n == 1 )
{
System.out.println(count);
return 0 ;
}
if (count == len || a - b == 0 )
{
System.out.println(count * n);
return 0 ;
}
int n2 = n - 1 , count2 = 0 ;
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;
}
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)) ;
}
}
|
Python3
def prefix(k, n):
a = 0
b = 0
count = 0
i = 0
Len = len (k)
for i in range ( Len ):
if (k[i] = = "a" ):
a + = 1
if (k[i] = = "b" ):
b + = 1
if (a > b) :
count + = 1
if (count = = 0 or n = = 1 ):
print (count)
return 0
if (count = = Len or a - b = = 0 ) :
print (count * n)
return 0
n2 = n - 1
count2 = 0
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
S = "aba"
N = 2
print (prefix(S, N))
S = "baa"
N = 3
print (prefix(S, N))
|
C#
using System;
class GFG
{
static int prefix(String k, int n)
{
int a = 0, b = 0,
count = 0;
int i = 0;
int len = k.Length;
for (i = 0; i < len; i++)
{
if (k[i] == 'a' )
a++;
if (k[i] == 'b' )
b++;
if (a > b)
{
count++;
}
}
if (count == 0 || n == 1)
{
Console.WriteLine(count);
return 0;
}
if (count == len ||
a - b == 0)
{
Console.WriteLine(count * n);
return 0;
}
int n2 = n - 1, count2 = 0;
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;
}
public static void Main ()
{
string S = "aba" ;
int N = 2;
Console.WriteLine(prefix(S, N));
S = "baa" ;
N = 3;
Console.WriteLine(prefix(S, N)) ;
}
}
|
PHP
<?php
function prefix( $k , $n )
{
$a = 0; $b = 0; $count = 0;
$i = 0;
$len = strlen ( $k );
for ( $i = 0; $i < $len ; $i ++)
{
if ( $k [ $i ] == 'a' )
$a ++;
if ( $k [ $i ] == 'b' )
$b ++;
if ( $a > $b )
{
$count ++;
}
}
if ( $count == 0 || $n == 1)
{
echo ( $count );
return 0;
}
if ( $count == $len || $a - $b == 0)
{
echo ( $count * $n );
return 0;
}
$n2 = $n - 1; $count2 = 0;
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 ;
}
$S = "aba" ;
$N = 2;
echo (prefix( $S , $N ). "\n" );
$S = "baa" ;
$N = 3;
echo (prefix( $S , $N ). "\n" );
|
Javascript
<script>
function prefix(k,n)
{
let a = 0, b = 0,
count = 0;
let i = 0;
let len = k.length;
for (i = 0; i < len; i++)
{
if (k[i] == 'a' )
a++;
if (k[i] == 'b' )
b++;
if (a > b)
{
count++;
}
}
if (count == 0 || n == 1)
{
document.write(count+ "<br>" );
return 0;
}
if (count == len || a - b == 0)
{
document.write((count * n)+ "<br>" );
return 0;
}
let n2 = n - 1, count2 = 0;
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;
}
let S = "aba" ;
let N = 2;
document.write(prefix(S, N)+ "<br>" );
S = "baa" ;
N = 3;
document.write(prefix(S, N)+ "<br>" ) ;
</script>
|
Time Complexity: O(L * N), where L is the length of the given string and N is the given input.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...