Minimum operations require to balance Binary String
Last Updated :
06 Dec, 2023
Given a binary string S of length N, the task is to find the minimum number of operations required to balance (equal number of '0' and '1') the given binary string, you can perform two types of operations one at a time, which is given below.
- Remove a character from the front of the given string.
- Remove a character from the end of the given string.
Examples:
Input: S = "0111000"
Output: 1
Explanation: Remove 1 character from the front of the given string.
Input: S = "11100100"
Output: 0
If we assign value -1 to '0' characters and the value of +1 to '1' characters, then any valid substring in which the count of '0's and '1's are equal will result in zero sum. Now, the only task is to determine the length of the longest substring whose sum would be zero. The remaining character n - length of the longest substring with 0 sum would be the result.
Steps to solve this problem:
- Create a map unmap to store the prefix sums with their first occurrence indices.
- Iterate through the characters in the string s:
- For each character, update the prefixSum by +1 if the character is '1', or -1 if the character is '0'.
- If the prefixSum becomes zero at any point, update result by (i + 1), this ensures that this is the longest valid substring encountered so far.
- Check if the current prefixSum value exists in the unmap.
- If true, update result again with the maximum of its current value and i - unmap[prefixSum], which calculates the length of the current valid substring.
- Otherwise, store current prefixSum in unmap with the index i.
- Finally, return the remaining length of the string, which is n - result.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach.
#include <bits/stdc++.h>
using namespace std;
// Function to solve the problem
int minOperation(string& s)
{
// Storing prefixSum with index
// of its first occurance
unordered_map<int, int> unmap;
int n = s.size();
// For storing the prefix Sum
// ending at ith index
int prefixSum = 0;
// For keeping the length of
// longest binary string where
// number of zero and ones are equal
int result = 0;
// Iterate over the string
for (int i = 0; i < n; i++) {
prefixSum += ((s[i] == '1') ? 1 : -1);
if (prefixSum == 0) {
result = max(result, i + 1);
}
// Check if prefixSum have
// previously occured or not
if (unmap.count(prefixSum)) {
// Update the result with
// this valid substring
result = max(result, i - unmap[prefixSum]);
}
else {
// Store this prefixSum has
// occur at ith index
// in the map.
unmap[prefixSum] = i;
}
}
// Return the remaining length
// other than the longest
// valid substring.
return n - result;
}
// Driver code
int main()
{
string S = "0111000";
// Function call
int result = minOperation(S);
cout << result << endl;
return 0;
}
Java
import java.util.HashMap;
public class Main {
// Function to solve the problem
public static int minOperation(String s)
{
// Storing prefixSum with the index of its first
// occurrence
HashMap<Integer, Integer> unmap = new HashMap<>();
int n = s.length();
// For storing the prefix Sum ending at the ith
// index
int prefixSum = 0;
// For keeping the length of the longest binary
// string where the number of zeros and ones are
// equal
int result = 0;
// Iterate over the string
for (int i = 0; i < n; i++) {
prefixSum += (s.charAt(i) == '1' ? 1 : -1);
if (prefixSum == 0) {
result = Math.max(result, i + 1);
}
// Check if prefixSum has previously occurred or
// not
if (unmap.containsKey(prefixSum)) {
// Update the result with this valid
// substring
result = Math.max(result,
i - unmap.get(prefixSum));
}
else {
// Store this prefixSum has occurred at the
// ith index in the map.
unmap.put(prefixSum, i);
}
}
// Return the remaining length other than the
// longest valid substring.
return n - result;
}
// Driver code
public static void main(String[] args)
{
String S = "0111000";
// Function call
int result = minOperation(S);
System.out.println(result);
}
}
Python3
def minOperation(s):
# Storing prefixSum with index
# of its first occurrence
unmap = {}
n = len(s)
# For storing the prefix Sum
# ending at ith index
prefixSum = 0
# For keeping the length of
# the longest binary string where
# the number of zeros and ones are equal
result = 0
# Iterate over the string
for i in range(n):
if s[i] == '1':
prefixSum += 1
else:
prefixSum -= 1
if prefixSum == 0:
result = max(result, i + 1)
# Check if prefixSum has
# previously occurred or not
if prefixSum in unmap:
# Update the result with
# this valid substring
result = max(result, i - unmap[prefixSum])
else:
# Store this prefixSum has
# occurred at ith index
# in the map.
unmap[prefixSum] = i
# Return the remaining length
# other than the longest
# valid substring.
return n - result
# Driver code
if __name__ == "__main__":
S = "0111000"
# Function call
result = minOperation(S)
print(result)
C#
using System;
using System.Collections.Generic;
class Program
{
// Function to solve the problem
static int MinOperation(string s)
{
// Storing prefixSum with index
// of its first occurrence
Dictionary<int, int> unmap = new Dictionary<int, int>();
int n = s.Length;
// For storing the prefix Sum
// ending at ith index
int prefixSum = 0;
// For keeping the length of
// longest binary string where
// number of zero and ones are equal
int result = 0;
// Iterate over the string
for (int i = 0; i < n; i++)
{
prefixSum += (s[i] == '1') ? 1 : -1;
if (prefixSum == 0)
{
result = Math.Max(result, i + 1);
}
// Check if prefixSum has
// previously occurred or not
if (unmap.ContainsKey(prefixSum))
{
// Update the result with
// this valid substring
result = Math.Max(result, i - unmap[prefixSum]);
}
else
{
// Store this prefixSum has
// occurred at ith index
// in the map.
unmap[prefixSum] = i;
}
}
// Return the remaining length
// other than the longest
// valid substring.
return n - result;
}
// Driver code
static void Main()
{
string S = "0111000";
// Function call
int result = MinOperation(S);
Console.WriteLine(result);
}
}
JavaScript
function MinOperation(s) {
// Storing prefixSum with index
// of its first occurrence
let unmap = new Map();
let n = s.length;
// For storing the prefix Sum
// ending at ith index
let prefixSum = 0;
// For keeping the length of
// longest binary string where
// number of zeros and ones are equal
let result = 0;
// Iterate over the string
for (let i = 0; i < n; i++) {
prefixSum += (s[i] === '1') ? 1 : -1;
if (prefixSum === 0) {
result = Math.max(result, i + 1);
}
// Check if prefixSum has
// previously occurred or not
if (unmap.has(prefixSum)) {
// Update the result with
// this valid substring
result = Math.max(result, i - unmap.get(prefixSum));
} else {
// Store this prefixSum has
// occurred at ith index
// in the map.
unmap.set(prefixSum, i);
}
}
// Return the remaining length
// other than the longest
// valid substring.
return n - result;
}
// Driver code
let S = "0111000";
// Function call
let result = MinOperation(S);
console.log(result);
Time Complexity: O(N), where N is the length of the given binary string.
Auxiliary Space: O(N)
Similar Reads
Minimum operations required to convert a binary string to all 0s or all 1s Given a binary string str, the task is to find the minimum number of operations required to make all the characters of the string same i.e. either the resultant string contains all 0s or all 1s. In a single operation, any block of consecutive 0s can be converted to a block of consecutive 1s of the s
4 min read
Minimum number of operations required to obtain a given Binary String Given a binary strings S of length N, the task is to obtain S from a string, say T, of length N consisting only of zeroes, by minimum number of operations. Each operation involves choosing any index i from string S and flipping all the bits at indices [i, N - 1] of the string T. Examples: Input: S =
8 min read
Minimum number of operations required to sum to binary string S Given a binary string, S. Find the minimum number of operations required to be performed on the number zero to convert it to the number represented by S.It is allowed to perform operations of 2 types: Add 2xSubtract 2xNote: Start performing operations on 0. Examples: Input : 100 Output : 1 Explanati
15+ min read
Minimum given operations required to convert a given binary string to all 1's Given a binary number as a string str of length L. The task is to find the minimum number of operations needed so that the number becomes 2L-1, that is a string consisting of only 1's of the length L. In each operation, the number N can be replaced by N xor (N + 1). Examples: Input: str = "10010111"
7 min read
Minimum removals required to place all 0s before 1s in a Binary String Given a binary string S, the task is to find the minimum number of characters required to be removed from S, such that all the 0s are placed before 1s. Examples: Input: S = "001101"Output: 1Explanation: Removing S[4] (= '0') modifies the string S to "00111".Therefore, minimum number of deletions req
6 min read