Given a binary string str, the task is to find the minimum number of characters in the string that have to be replaced in order to make the string alternating (i.e. of the form 01010101… or 10101010…).
Examples:
Input: str = “1100”
Output: 2
Replace 2nd character with ‘0’ and 3rd character with ‘1’
Input: str = “1010”
Output: 0
The string is already alternating.
We have discussed one approach in Number of flips to make binary string alternate. In this post a better approach is discussed.
Approach: For the string str, there can be two possible solutions. Either the resultant string can be
- 010101… or
- 101010…
In order to find the minimum replacements, count the number of replacements to convert the string in type 1 and store it in count then minimum replacement will be min(count, len – count) where len is the length of the string. len – count is the number of replacements to convert the string in type 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minReplacement(string s, int len)
{
int ans = 0;
for ( int i = 0; i < len; i++) {
if (i % 2 == 0 && s[i] == '1' )
ans++;
if (i % 2 == 1 && s[i] == '0' )
ans++;
}
return min(ans, len - ans);
}
int main()
{
string s = "1100" ;
int len = s.size();
cout << minReplacement(s, len);
return 0;
}
|
Java
class GFG {
static int minReplacement(String s, int len)
{
int ans = 0 ;
for ( int i = 0 ; i < len; i++) {
if (i % 2 == 0 && s.charAt(i) == '1' )
ans++;
if (i % 2 == 1 && s.charAt(i) == '0' )
ans++;
}
return Math.min(ans, len - ans);
}
public static void main(String args[])
{
String s = "1100" ;
int len = s.length();
System.out.print(minReplacement(s, len));
}
}
|
Python3
def minReplacement(s, length):
ans = 0
for i in range ( 0 , length):
if i % 2 = = 0 and s[i] = = '1' :
ans + = 1
if i % 2 = = 1 and s[i] = = '0' :
ans + = 1
return min (ans, length - ans)
if __name__ = = "__main__" :
s = "1100"
length = len (s)
print (minReplacement(s, length))
|
C#
using System;
class GFG
{
static int minReplacement(String s, int len)
{
int ans = 0;
for ( int i = 0; i < len; i++)
{
if (i % 2 == 0 && s[i] == '1' )
ans++;
if (i % 2 == 1 && s[i] == '0' )
ans++;
}
return Math.Min(ans, len - ans);
}
public static void Main(String []args)
{
String s = "1100" ;
int len = s.Length;
Console.Write(minReplacement(s, len));
}
}
|
PHP
<?php
function minReplacement( $s , $len )
{
$ans = 0;
for ( $i = 0; $i < $len ; $i ++)
{
if ( $i % 2 == 0 && $s [ $i ] == '1' )
$ans ++;
if ( $i % 2 == 1 && $s [ $i ] == '0' )
$ans ++;
}
return min( $ans , $len - $ans );
}
$s = "1100" ;
$len = strlen ( $s );
echo minReplacement( $s , $len );
?>
|
Javascript
<script>
function minReplacement(s, len)
{
var ans = 0;
for ( var i = 0; i < len; i++) {
if (i % 2 == 0 && s[i] == '1' )
ans++;
if (i % 2 == 1 && s[i] == '0' )
ans++;
}
return Math.min(ans, len - ans);
}
var s = "1100" ;
var len = s.length;
document.write(minReplacement(s, len));
</script>
|
Time Complexity: O(len), where len is the length of the given string. We are traversing till len.
Auxiliary Space: O(1), as we are not using any extra space.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Apr, 2022
Like Article
Save Article