Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0
Last Updated :
12 Aug, 2021
Given a binary string S, the task is to find the maximum length of substrings required to be flipped repeatedly to make all the characters of a binary string equal to ‘0’.
Examples:
Input: S = “010”
Output: 2
Explanation:
Following are the order of flipping of substring of at least K for the value of K as 2:
- Flip the substring S[0, 2] of length 3(>= K) modifies the string S to “101”.
- Flip the substring S[0, 1] of length 2(>= K) modifies the string S to “011”.
- Flip the substring S[1, 2] of length 2(>= K) modifies the string S to “000”.
For the value of K as 2(which is the maximum possible value) all the characters of the string can be made 0. Therefore, print 2.
Input: S = “00001111”
Output: 4
Approach: The given problem can be solved by traversing the given string S, now if at any point the adjacent characters are not the same then flip one sub-string LHS or RHS. For that, take the maximum length of LHS and RHS. There can be multiple adjacent places where characters are not equal. For each pair of substrings, the maximum required will be different. Now to change all the characters to ‘0’ take the minimum among all those maximums. Follow the steps below to solve the problem:
- Initialize the variable, say ans as N that stores the maximum possible value of K.
- Iterate over the range [0, N – 1) using the variable i and perform the following steps:
- If the value of s[i] and s[i + 1] are not the same, then update the value of ans to the minimum of ans or maximum of (i + 1) or (N – i – 1).
- After performing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumK(string& S)
{
int N = S.length();
int ans = N;
int flag = 0;
for ( int i = 0; i < N - 1; i++) {
if (S[i] != S[i + 1]) {
flag = 1;
ans = min(ans, max(i + 1,
N - i - 1));
}
}
if (flag == 0)
return 0;
return ans;
}
int main()
{
string S = "010" ;
cout << maximumK(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maximumK(String S)
{
int N = S.length();
int ans = N;
int flag = 0 ;
for ( int i = 0 ; i < N - 1 ; i++) {
if (S.charAt(i) != S.charAt(i + 1 )) {
flag = 1 ;
ans = Math.min(ans, Math.max(i + 1 ,
N - i - 1 ));
}
}
if (flag == 0 )
return 0 ;
return ans;
}
public static void main(String[] args)
{
String S = "010" ;
System.out.print(maximumK(S));
}
}
|
Python3
def maximumK(S):
N = len (S)
ans = N
flag = 0
for i in range (N - 1 ):
if (S[i] ! = S[i + 1 ]):
flag = 1
ans = min (ans, max (i + 1 ,N - i - 1 ))
if (flag = = 0 ):
return 0
return ans
if __name__ = = '__main__' :
S = "010"
print (maximumK(S))
|
C#
using System;
public class GFG {
static int maximumK(String S)
{
int N = S.Length;
int ans = N;
int flag = 0;
for ( int i = 0; i < N - 1; i++) {
if (S[i] != S[i + 1]) {
flag = 1;
ans = Math.Min(ans,
Math.Max(i + 1, N - i - 1));
}
}
if (flag == 0)
return 0;
return ans;
}
static public void Main()
{
string S = "010" ;
Console.Write(maximumK(S));
}
}
|
Javascript
<script>
function maximumK(S)
{
let N = S.length;
let ans = N;
let flag = 0;
for (let i = 0; i < N - 1; i++)
{
if (S[i] != S[i + 1])
{
flag = 1;
ans = Math.min(ans, Math.max(i + 1, N - i - 1));
}
}
if (flag == 0) return 0;
return ans;
}
let S = "010" ;
document.write(maximumK(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...