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 = “101”
Output: 3
Explanation:
“000” -> “111” -> “100” -> “101”.
Therefore, the minimum number of steps required is 3.
Input: S = “10111”
Output: 3
Explanation:
“00000” -> “11111” -> “10000” -> “10111”.
Therefore, the minimum number of steps required is 3.
Approach:
The idea is to find the first occurrence of 1 in the given string S and perform the given operation at that index. After this step, for every mismatch in the character of S and T at a particular index, repeat the operation.
Follow the steps below:
- Iterate over S and mark the first occurrence of 1.
- Initialize two variables, say last and ans, where last stores the character (0 or 1) for which the last operation was performed and ans keeps count of the minimum number of steps required.
- Iterate from the first occurrence of 1 till the end of the string.
- If the current character is 0 and last = 1, then increment the count of ans, and set last = 0.
- Otherwise, if last = 0 and the current character is 1, then set last = 1.
- Return the final value of ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations(string s)
{
int n = s.size();
int pos = -1;
for ( int i = 0; i < s.size(); i++) {
if (s[i] == '1' ) {
pos = i;
break ;
}
}
if (pos == -1) {
return 0;
}
int last = 1;
int ans = 1;
for ( int i = pos + 1; i < n; i++) {
if (s[i] == '0' ) {
if (last == 1) {
ans++;
last = 0;
}
}
else {
if (last == 0) {
ans++;
last = 1;
}
}
}
return ans;
}
int main()
{
string s = "10111" ;
cout << minOperations(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minOperations(String s)
{
int n = s.length();
int pos = - 1 ;
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) == '1' )
{
pos = i;
break ;
}
}
if (pos == - 1 )
{
return 0 ;
}
int last = 1 ;
int ans = 1 ;
for ( int i = pos + 1 ; i < n; i++)
{
if (s.charAt(i) == '0' )
{
if (last == 1 )
{
ans++;
last = 0 ;
}
}
else
{
if (last == 0 )
{
ans++;
last = 1 ;
}
}
}
return ans;
}
public static void main(String[] args)
{
String s = "10111" ;
System.out.println(minOperations(s));
}
}
|
Python3
def minOperations(s):
n = len (s)
pos = - 1
for i in range ( len (s)):
if (s[i] = = '1' ):
pos = i
break
if (pos = = - 1 ):
return 0
last = 1
ans = 1
for i in range (pos + 1 , n):
if (s[i] = = '0' ):
if (last = = 1 ):
ans + = 1
last = 0
else :
if (last = = 0 ):
ans + = 1
last = 1
return ans
if __name__ = = "__main__" :
s = "10111"
print (minOperations(s))
|
C#
using System;
class GFG{
static int minOperations(String s)
{
int n = s.Length;
int pos = -1;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '1' )
{
pos = i;
break ;
}
}
if (pos == -1)
{
return 0;
}
int last = 1;
int ans = 1;
for ( int i = pos + 1; i < n; i++)
{
if (s[i] == '0' )
{
if (last == 1)
{
ans++;
last = 0;
}
}
else
{
if (last == 0)
{
ans++;
last = 1;
}
}
}
return ans;
}
public static void Main( string [] args)
{
String s = "10111" ;
Console.Write(minOperations(s));
}
}
|
Javascript
<script>
function minOperations(s)
{
let n = s.length;
let pos = -1;
for (let i = 0; i < s.length; i++) {
if (s[i] == '1' ) {
pos = i;
break ;
}
}
if (pos == -1) {
return 0;
}
let last = 1;
let ans = 1;
for (let i = pos + 1; i < n; i++) {
if (s[i] == '0' ) {
if (last == 1) {
ans++;
last = 0;
}
}
else {
if (last == 0) {
ans++;
last = 1;
}
}
}
return ans;
}
let s = "10111" ;
document.write(minOperations(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
04 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...