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:
- If all the characters are equal, then it is impossible to make the string balanced. Therefore, print “-1”.
- Store the count of 1s and 0s in variables, say count1 and count0 respectively.
- Store the absolute difference of count1 and count0 in a variable K.
- Now, flip the K/2 characters to make the string balanced and print the value of K/2 as the result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the minimum cost // to convert the given string into // balanced string void findMinimumCost(string s, int N)
{ // Stores count of 1's and 0's in
// the string
int count_1 = 0, count_0 = 0;
// Traverse the string
for ( int i = 0; i < N; i++) {
if (s[i] == '1' )
// Increment count1
count_1++;
else
// Increment count 0
count_0++;
}
// Stores absolute difference of
// counts of 0's and 1's
int k = abs (count_0 - count_1);
// If string consists of only
// 0's and 1's
if (count_1 == N || count_0 == N)
cout << -1 << endl;
// Print minimum cost
else
cout << k / 2 << endl;
} // Driver Code int main()
{ string S = "110110" ;
int N = S.length();
findMinimumCost(S, N);
return 0;
} |
// Java program for the above approach import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the minimum cost // to convert the given string into // balanced string static void findMinimumCost(String s, int N)
{ // Stores count of 1's and 0's in
// the string
int count_1 = 0 , count_0 = 0 ;
// Traverse the string
for ( int i = 0 ; i < N; i++) {
if (s.charAt(i) == '1' )
// Increment count1
count_1++;
else
// Increment count 0
count_0++;
}
// Stores absolute difference of
// counts of 0's and 1's
int k = Math.abs(count_0 - count_1);
// If string consists of only
// 0's and 1's
if (count_1 == N || count_0 == N)
System.out.println( - 1 );
// Print minimum cost
else
System.out.println( k / 2 );
} // Driver Code public static void main(String[] args)
{ String S = "110110" ;
int N = S.length();
findMinimumCost(S, N);
} } // This code is contributed by code_hunt. |
# Python3 program for the above approach # Function to find the minimum cost # to convert the given into # balanced string def findMinimumCost(s, N):
# Stores count of 1's and 0's in
# the string
count_1, count_0 = 0 , 0
# Traverse the string
for i in range (N):
if (s[i] = = '1' ):
# Increment count1
count_1 + = 1
else :
# Increment count 0
count_0 + = 1
# Stores absolute difference of
# counts of 0's and 1's
k = abs (count_0 - count_1)
# If consists of only
# 0's and 1's
if (count_1 = = N or count_0 = = N):
print ( - 1 )
# Print the minimum cost
else :
print (k / / 2 )
# Driver Code if __name__ = = '__main__' :
S = "110110"
N = len (S)
findMinimumCost(S, N)
# This code is contributed by mohit kumar 29 |
// C# program for the above approach using System;
class GFG{
// Function to find the minimum cost // to convert the given string into // balanced string static void findMinimumCost(String s, int N)
{ // Stores count of 1's and 0's in
// the string
int count_1 = 0, count_0 = 0;
// Traverse the string
for ( int i = 0; i < N; i++)
{
if (s[i] == '1' )
// Increment count1
count_1++;
else
// Increment count 0
count_0++;
}
// Stores absolute difference of
// counts of 0's and 1's
int k = Math.Abs(count_0 - count_1);
// If string consists of only
// 0's and 1's
if (count_1 == N || count_0 == N)
Console.WriteLine(-1);
// Print minimum cost
else
Console.WriteLine(k / 2);
} // Driver Code static public void Main()
{ String S = "110110" ;
int N = S.Length;
findMinimumCost(S, N);
} } // This code is contributed by Dharanendra L V. |
<script> // JavaScript program for the above approach // Function to find the minimum cost // to convert the given string into // balanced string function findMinimumCost(s, N)
{ // Stores count of 1's and 0's in
// the string
let count_1 = 0, count_0 = 0;
// Traverse the string
for (let i = 0; i < N; i++) {
if (s[i] == '1' )
// Increment count1
count_1++;
else
// Increment count 0
count_0++;
}
// Stores absolute difference of
// counts of 0's and 1's
let k = Math.abs(count_0 - count_1);
// If string consists of only
// 0's and 1's
if (count_1 == N || count_0 == N)
document.write( -1);
// Print minimum cost
else
document.write( k / 2);
} // Driver Code let S = "110110" ;
let N = S.length;
findMinimumCost(S, N);
</script> |
1
Time Complexity: O(N)
Auxiliary Space: O(1)