Minimize cost of flipping or swaps to make a Binary String balanced
Last Updated :
07 Sep, 2021
Given a binary string S of size N(where N is even), the task is to find the minimum cost of making the given binary string balanced by flipping one of the adjacent different characters at the cost of 1 unit or swap the characters at indices i and j such that (i < j) at the cost of (j – i) unit. If it is impossible to make the string balanced, the print “-1”.
A binary string is balanced if it can be reduced to an empty string by deleting two consecutive alternating characters at once and then concatenating the remaining characters.
Examples:
Input: S = “110110”
Output: 1
Explanation: Following operations are performed:
Operation 1: As the adjacent characters at indices 0 and 1 are different, flipping S[0] modifies S to “010110”. Cost = 1.
After completing the above operations, the given string becomes balanced, as it can be reduced to an empty string by deleting two consecutive alternating characters.
Therefore, the total cost is 1.
Input: S = “11100”
Output: -1
Approach: The given problem can be solved based on the observations that the flipping of the adjacent different characters is more optimal than using the swapping of characters and the position of 0s and 1s in the string does not matter. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinimumCost(string s, int N)
{
int count_1 = 0, count_0 = 0;
for ( int i = 0; i < N; i++) {
if (s[i] == '1' )
count_1++;
else
count_0++;
}
int k = abs (count_0 - count_1);
if (count_1 == N || count_0 == N)
cout << -1 << endl;
else
cout << k / 2 << endl;
}
int main()
{
string S = "110110" ;
int N = S.length();
findMinimumCost(S, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void findMinimumCost(String s, int N)
{
int count_1 = 0 , count_0 = 0 ;
for ( int i = 0 ; i < N; i++) {
if (s.charAt(i) == '1' )
count_1++;
else
count_0++;
}
int k = Math.abs(count_0 - count_1);
if (count_1 == N || count_0 == N)
System.out.println( - 1 );
else
System.out.println( k / 2 );
}
public static void main(String[] args)
{
String S = "110110" ;
int N = S.length();
findMinimumCost(S, N);
}
}
|
Python3
def findMinimumCost(s, N):
count_1, count_0 = 0 , 0
for i in range (N):
if (s[i] = = '1' ):
count_1 + = 1
else :
count_0 + = 1
k = abs (count_0 - count_1)
if (count_1 = = N or count_0 = = N):
print ( - 1 )
else :
print (k / / 2 )
if __name__ = = '__main__' :
S = "110110"
N = len (S)
findMinimumCost(S, N)
|
C#
using System;
class GFG{
static void findMinimumCost(String s, int N)
{
int count_1 = 0, count_0 = 0;
for ( int i = 0; i < N; i++)
{
if (s[i] == '1' )
count_1++;
else
count_0++;
}
int k = Math.Abs(count_0 - count_1);
if (count_1 == N || count_0 == N)
Console.WriteLine(-1);
else
Console.WriteLine(k / 2);
}
static public void Main()
{
String S = "110110" ;
int N = S.Length;
findMinimumCost(S, N);
}
}
|
Javascript
<script>
function findMinimumCost(s, N)
{
let count_1 = 0, count_0 = 0;
for (let i = 0; i < N; i++) {
if (s[i] == '1' )
count_1++;
else
count_0++;
}
let k = Math.abs(count_0 - count_1);
if (count_1 == N || count_0 == N)
document.write( -1);
else
document.write( k / 2);
}
let S = "110110" ;
let N = S.length;
findMinimumCost(S, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...