Concatenate strings in any order to get Maximum Number of “AB”
Given an array of strings of length N, it is allowed to concatenate them in any order. Find the maximum possible number of occurrences of ‘AB’ in the resulting string.
Examples:
Input : N = 4, arr={ “BCA”, “BGGGA”, “JKA”, “BALB” }
Output : 3
Concatenate them in the order JKA + BGGA + BCA + BALB and it will become JKABGGABCABALB and it has 3 occurrences of ‘AB’.
Input : N = 3, arr={ “ABCA”, “BOOK”, “BAND” }
Output : 2
Approach:
Calculate the number of ABs within each string beforehand. Concentrate on the change of the number of ABs that extend over two strings when the strings are rearranged. The only characters that matter in each string are its first and last characters.
The strings that can contribute to the answer are:
- A string that begins with B and ends with A.
- A string that begins with B but does not end with A.
- A string that does not begin with B but ends with A.
Let c1, c2, and c3 be the number of strings of categories 1, 2, and 3, respectively.
- If c1 = 0, then the answer is min(c2, c3) as we can take both and concatenate as long as both are available.
- If c1 > 0 and c2 + c3 = 0, then the answer is c1 – 1 as we concatenate them in serial order one by one.
- If c1 > 0 and c2 + c3 > 0 and take min(c2, c3) = p, first concatenate the category 1 string one by one and the extra c1 – 1 ‘AB’ and then if both category 2 and 3 are available, then add category 3 at the beginning of present resulting string and add category 2 at the end of present resulting string.
- There are c1 – 1 + 2 = c1 + 1 extra ‘AB’ and now c2 and c3 decreased by one and p become p – 1 and now take both
category 2 and 3 and add them as long as both are available and now we get a total of c1 + 1 + (p – 1) = c1 + p extra ‘AB’. That means c1 + min(c2, c3) .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxCountAB(string s[], int n)
{
int A = 0, B = 0, BA = 0, ans = 0;
for ( int i = 0; i < n; i++) {
string S = s[i];
int L = S.size();
for ( int j = 0; j < L - 1; j++) {
if (S.at(j) == 'A' &&
S.at(j + 1) == 'B' ) {
ans++;
}
}
if (S.at(0) == 'B' && S.at(L - 1) == 'A' )
BA++;
else if (S.at(0) == 'B' )
B++;
else if (S.at(L - 1) == 'A' )
A++;
}
if (BA == 0)
ans += min(B, A);
else if (A + B == 0)
ans += BA - 1;
else
ans += BA + min(B, A);
return ans;
}
int main()
{
string s[] = { "ABCA" , "BOOK" , "BAND" };
int n = sizeof (s) / sizeof (s[0]);
cout << maxCountAB(s, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxCountAB(String s[], int n)
{
int A = 0 , B = 0 , BA = 0 , ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
String S = s[i];
int L = S.length();
for ( int j = 0 ; j < L - 1 ; j++)
{
if (S.charAt(j) == 'A' &&
S.charAt(j + 1 ) == 'B' )
{
ans++;
}
}
if (S.charAt( 0 ) == 'B' && S.charAt(L - 1 ) == 'A' )
BA++;
else if (S.charAt( 0 ) == 'B' )
B++;
else if (S.charAt(L - 1 ) == 'A' )
A++;
}
if (BA == 0 )
ans += Math.min(B, A);
else if (A + B == 0 )
ans += BA - 1 ;
else
ans += BA + Math.min(B, A);
return ans;
}
public static void main(String[] args)
{
String s[] = { "ABCA" , "BOOK" , "BAND" };
int n = s.length;
System.out.println(maxCountAB(s, n));
}
}
|
Python3
def maxCountAB(s,n):
A = 0
B = 0
BA = 0
ans = 0
for i in range (n):
S = s[i]
L = len (S)
for j in range (L - 1 ):
if (S[j] = = 'A' and S[j + 1 ] = = 'B' ):
ans + = 1
if (S[ 0 ] = = 'B' and S[L - 1 ] = = 'A' ):
BA + = 1
elif (S[ 0 ] = = 'B' ):
B + = 1
elif (S[L - 1 ] = = 'A' ):
A + = 1
if (BA = = 0 ):
ans + = min (B, A)
elif (A + B = = 0 ):
ans + = BA - 1
else :
ans + = BA + min (B, A)
return ans
if __name__ = = '__main__' :
s = [ "ABCA" , "BOOK" , "BAND" ]
n = len (s)
print (maxCountAB(s, n))
|
C#
using System;
class GFG
{
static int maxCountAB( string []s, int n)
{
int A = 0, B = 0, BA = 0, ans = 0;
for ( int i = 0; i < n; i++)
{
string S = s[i];
int L = S.Length;
for ( int j = 0; j < L - 1; j++)
{
if (S[j] == 'A' &&
S[j + 1] == 'B' )
{
ans++;
}
}
if (S[0] == 'B' && S[L - 1] == 'A' )
BA++;
else if (S[0] == 'B' )
B++;
else if (S[L - 1] == 'A' )
A++;
}
if (BA == 0)
ans += Math.Min(B, A);
else if (A + B == 0)
ans += BA - 1;
else
ans += BA + Math.Min(B, A);
return ans;
}
public static void Main()
{
string []s = { "ABCA" , "BOOK" , "BAND" };
int n = s.Length;
Console.WriteLine(maxCountAB(s, n));
}
}
|
Javascript
<script>
function maxCountAB(s, n)
{
var A = 0, B = 0, BA = 0, ans = 0;
for ( var i = 0; i < n; i++) {
var S = s[i];
var L = S.length;
for ( var j = 0; j < L - 1; j++) {
if (S[j] == 'A' &&
S[j + 1] == 'B' ) {
ans++;
}
}
if (S[0] == 'B ' && S[L - 1] == ' A ')
BA++;
// count of strings that begins
// with ' B ' but does not end with ' A '
else if (S[0] == ' B ')
B++;
// count of strings that ends with
// ' A ' but not end with ' B '
else if (S[L - 1] == ' A ')
A++;
}
// updating the value of ans and
// add extra count of ' AB'
if (BA == 0)
ans += Math.min(B, A);
else if (A + B == 0)
ans += BA - 1;
else
ans += BA + Math.min(B, A);
return ans;
}
var s = [ "ABCA" , "BOOK" , "BAND" ];
var n = s.length;
document.write( maxCountAB(s, n));
</script>
|
Time Complexity: O(N * L), where N is the size of the given string array and L is the maximum length of a string in the array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
21 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...