Minimum count of Inversion Pairs possible by concatenating N binary strings in any order
Given N strings in the form of array str, each of length M and containing only characters ‘a‘ and ‘b‘. The task is to find the count of minimum number of Inversion Pairs possible in the resultant strings formed by concatenating all N strings in any order, without changing the order of any individual string.
An Inversion Pair in a string S is a pair of indices (i,j) such that i<j and Si = ‘b’, Sj = ‘a’.
For example, the string S= “abababbbb” contains 3 inversions : (2,3), (2,5), (4,5).
Examples:
Input: N = 3 , M = 2, str = {“ba” , “aa” , “ab”}
Output: 2
Explanation: If we concatenate the strings in order s2 + s1 + s3 = “aabaab” , then the inversion pair will be at (3, 4) and (3 , 5)
Input: N = 2 , M = 2, str = {“b” , “b”}
Output: 0
Approach: This question can be solved by the Greedy Algorithm and the approach should be to keep the string with a higher count of character ‘b‘ at the right end.
Follow the below steps to solve the problem:
- Take a vector of strings of size n to receive the input.
- Sort the vector of strings using the comparators based on the count of ‘b‘ in a particular string.
- Take an empty string, Let us say res = “”.
- After that traverse, the sorted vector, add the current string to the string res.
- Traverse the string res and keep the count of occurrences of ‘b’ and whenever you encounter the character ‘a’ add the number of ‘b’ seen so far to the ans variable because with that ‘a’ you can make pairs up to a number of ‘b’ seen so far.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool cmp(string& s1, string& s2)
{
return count(s1.begin(), s1.end(), 'b' )
< count(s2.begin(), s2.end(), 'b' );
}
int findInversion(vector<string> arr, int N, int M)
{
sort(arr.begin(), arr.end(), cmp);
string res = "" ;
for ( int i = 0; i < N; i++) {
res = res + arr[i];
}
int cnt = 0;
int inversionPairs = 0;
for ( int i = 0; i < N * M; i++) {
if (res[i] == 'b' ) {
cnt += 1;
continue ;
}
else {
inversionPairs = inversionPairs + cnt;
}
}
return inversionPairs;
}
int main()
{
int N = 3, M = 2;
vector<string> arr = { "ba" , "aa" , "ab" };
int ans = findInversion(arr, N, M);
cout << ans << "\n" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int findInversion(List<String> arr, int N,
int M)
{
String res = "" ;
for ( int i = 0 ; i < N; i++) {
res = res + arr.get(i);
}
int cnt = 0 ;
int inversionPairs = 0 ;
for ( int i = 0 ; i < N * M; i++) {
if (res.charAt(i) == 'b' ) {
cnt += 1 ;
continue ;
}
else {
inversionPairs = inversionPairs + cnt;
}
}
return inversionPairs;
}
public static void main(String[] args)
{
int N = 3 , M = 2 ;
List<String> arr = new ArrayList<String>();
arr.add( "ba" );
arr.add( "aa" );
arr.add( "ab" );
Collections.sort(arr, new Comparator<String>() {
@Override
public int compare( final String s1, String s2)
{
int cnts1 = 0 ;
for ( int i = 0 ; i < s1.length(); i++) {
if ( 'b' == s1.charAt(i)) {
cnts1++;
}
}
int cnts2 = 0 ;
for ( int i = 0 ; i < s2.length(); i++) {
if ( 'b' == s2.charAt(i)) {
cnts2++;
}
}
int ans = - 1 ;
if (cnts1 > cnts2)
ans = 1 ;
return ans;
}
});
int ans = findInversion(arr, N, M);
System.out.println(ans);
}
}
|
Python3
def cmp (s):
s1 = s[ 0 ]
s2 = s[ 1 ]
b_in_s1 = 0
b_in_s2 = 0
for i in s1:
if (i = = 'b' ):
b_in_s1 + = 1
for i in s2:
if (i = = 'b' ):
b_in_s2 + = 1
if (b_in_s1 = = b_in_s2):
return 0
return 1 if b_in_s1 - b_in_s2 else - 1 ;
def findInversion(arr, N, M):
arr.sort(key = cmp )
res = "";
for i in range (N):
res = res + arr[i];
cnt = 0 ;
inversionPairs = 0 ;
for i in range (N * M):
if (res[i] = = 'b' ):
cnt + = 1 ;
continue ;
else :
inversionPairs = inversionPairs + cnt;
return inversionPairs;
N = 3
M = 2 ;
arr = [ "ba" , "aa" , "ab" ];
ans = findInversion(arr, N, M);
print (ans);
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
public class GFG {
public static int cmp( string s1, string s2)
{
int cnts1 = 0;
for ( int i = 0; i < s1.Length; i++) {
if ( 'b' == s1[i]) {
cnts1++;
}
}
int cnts2 = 0;
for ( int i = 0; i < s2.Length; i++) {
if ( 'b' == s2[i]) {
cnts2++;
}
}
int ans = 0;
if (cnts1 > cnts2)
ans = 1;
return ans;
}
public static int findInversion(List< string > arr, int N,
int M)
{
arr.Sort(cmp);
string res = "" ;
for ( int i = 0; i < N; i++) {
res = res + arr[i];
}
int cnt = 0;
int inversionPairs = 0;
for ( int i = 0; i < N * M; i++) {
if (res[i] == 'b' ) {
cnt += 1;
continue ;
}
else {
inversionPairs = inversionPairs + cnt;
}
}
return inversionPairs;
}
static public void Main()
{
int N = 3, M = 2;
List< string > arr = new List< string >();
arr.Add( "ba" );
arr.Add( "aa" );
arr.Add( "ab" );
int ans = findInversion(arr, N, M);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function cmp(s1, s2) {
let b_in_s1 = 0
let b_in_s2 = 0
for (i of s1)
if (i == 'b' )
b_in_s1++;
for (i of s2)
if (i == 'b' )
b_in_s2++;
return b_in_s1 - b_in_s2;
}
function findInversion(arr, N, M)
{
arr.sort(cmp);
let res = "" ;
for (let i = 0; i < N; i++) {
res = res + arr[i];
}
let cnt = 0;
let inversionPairs = 0;
for (let i = 0; i < N * M; i++) {
if (res[i] == 'b' ) {
cnt += 1;
continue ;
}
else {
inversionPairs = inversionPairs + cnt;
}
}
return inversionPairs;
}
let N = 3, M = 2;
let arr = [ "ba" , "aa" , "ab" ];
let ans = findInversion(arr, N, M);
document.write(ans);
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N*M)
Last Updated :
06 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...