Minimize replacement of bits to make the count of 01 substring equal to 10 substring
Last Updated :
19 Jan, 2023
Given a binary string str. The task is to minimize the number of replacements of ‘0’ by ‘1’ or ‘1’ by ‘0’ to balance the binary string. A binary string is said to be balanced: “if the number of “01” substring = number of “10” substring”.
Examples:
Input: str = “101010”
Output: 1
Explanation: “01” = 2 & “10” = 3. So change the last character to ‘1’. The modified string will be “101011” or “001010”.
Input: str = “0000100” // balanced, “01” = 1 & “10” = 1
Output: 0
Explanation: The string is already balanced.
Approach: One can notice that “the balanced binary string will always have it’s first character equals to last character of string.”
Only one step is required to balance it, that is s.back() = s.front(). See the proof provided below
Proof:
Above Approach can be proved by using Principle of Mathematical Induction.
NOTE: In below proof, all the cases where first character equals to last character are considered.
for n = 0 —> empty string “” // count of “10” = 0 & count of “01” = 0
for n = 1 —> “0” or “1” // count of “10” = 0 & count of “01” = 0
for n = 2 —> “00” or “11” // count of “10” = 0 & count of “01” = 0
for n = 3
—> “000” // count of “10” = 0 & count of “01” = 0
or “111” // count of “10” = 0 & count of “01” = 0
or “010” // count of “10” = 1 & count of “01” = 1
or “101” // count of “10” = 1 & count of “01” = 1
Hence, By the principle of mathematical induction it will be true for every n, where n is a natural number.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimizeReplacements(string str)
{
unordered_map<string, int > count;
string temp;
for ( int i = 0; i <
str.length() - 1; i++) {
temp = str.substr(i, 2);
count[temp]++;
}
return abs (count[ "10" ] - count[ "01" ]);
}
int main()
{
string str = "101010" ;
cout << minimizeReplacements(str) << endl;
return 0;
}
|
Java
import java.util.HashMap;
class GFG{
static int minimizeReplacements(String str)
{
HashMap<String,
Integer> count = new HashMap<String,
Integer>();
String temp;
for ( int i = 0 ; i < str.length() - 1 ; i++)
{
temp = str.substring(i, i + 2 );
if (count.containsKey(temp))
count.put(temp, count.get(temp) + 1 );
else
count.put(temp, 1 );
}
return Math.abs(count.get( "10" ) - count.get( "01" ));
}
public static void main(String args[])
{
String str = "101010" ;
System.out.print(minimizeReplacements(str));
}
}
|
Python3
def minimizeReplacements( str ):
count = {}
temp = ""
for i in range ( 0 , len ( str ) - 1 ):
temp = str [i: i + 2 ]
if temp in count:
count[temp] = count[temp] + 1
else :
count[temp] = 1
return abs (count[ "10" ] - count[ "01" ])
str = "101010"
print (minimizeReplacements( str ))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int minimizeReplacements( string str)
{
Dictionary< string , int > count
= new Dictionary< string , int >();
string temp;
for ( int i = 0; i < str.Length - 1; i++) {
temp = str.Substring(i, 2);
if (count.ContainsKey(temp))
count[temp]++;
else
count[temp] = 1;
}
return Math.Abs(count[ "10" ] - count[ "01" ]);
}
public static void Main()
{
string str = "101010" ;
Console.WriteLine(minimizeReplacements(str));
}
}
|
Javascript
<script>
const minimizeReplacements = (str) => {
count = {};
let temp = "" ;
for (let i = 0; i <
str.length - 1; i++) {
temp = str.substring(i, i + 2);
if (temp in count) count[temp]++;
else count[temp] = 1;
}
return Math.abs(count[ "10" ] - count[ "01" ]);
}
let str = "101010" ;
document.write(minimizeReplacements(str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N), for the map used.
Share your thoughts in the comments
Please Login to comment...