Given a binary string S, and a substring K, the task is to find the minimum no of steps required to flip the characters in a binary string such that it doesn’t contain the given substring K. Note: In one step we can change 0 to 1 or vice versa.
Examples:
Input: S = “0111011”, K = “011”
Output: 2
Explanation:
In the above string we have the substring 011 for 2 times, hence change the 3rd bit and 7th bit.
Input: S = “010010”, K = “0100”
Output: 1
Explanation:
In the above string we have the substring 0100 for 1 time, change the 4th bit of the string to 1.
Naive Approach: The naive approach is to use the Pattern Searching. Iterate through the string for N! times(N = length of the binary string). In every iteration check for the substring K and if its a match then increment the count. At last, print the count which will be the number of steps required to make the string such that it doesn’t contain the given substring K.
Time Complexity: O(M*N) where M is the length of substring and N is the length of the binary string.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above method, the idea is to replace the substring with an empty string and subtract the resultant string length from original string length. After this divide the resultant string with the length of the given substring K to get the minimum no of steps required to flip the characters of the given string S so that it doesn’t contain the given substring K.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
#include <boost/algorithm/string/replace.hpp>
using namespace std;
int flipCharacters(string b,
string sub)
{
int res = b.size();
boost::replace_all(b, sub, "" );
res = res - b.size();
int temp = res / sub.size();
return temp;
}
int main()
{
string S = "010010" ;
string K = "0100" ;
int result = flipCharacters(S, K);
cout << (result);
}
|
Java
import java.util.*;
public class Test {
static int flipCharacters(String b,
String sub)
{
int res = b.length()
- b.replaceAll(sub, "" )
.length();
int temp = res / sub.length();
return temp;
}
public static void main(String[] args)
{
String S = "010010" ;
String K = "0100" ;
int result = flipCharacters(S, K);
System.out.println(result);
}
}
|
Python3
def flipCharacters(b, sub):
b1 = b.replace(sub, "")
n = int (( len (b) - len (b1)) / len (sub))
return n
if __name__ = = '__main__' :
S = "010010"
X = "0100"
result = flipCharacters(S, X)
print (result)
|
C#
using System;
class GFG
{
static int flipCharacters( string b,
string sub)
{
int res = b.Length -
b.Replace(sub, "" ).Length;
int temp = res / sub.Length;
return temp;
}
public static void Main( string [] args)
{
string S = "010010" ;
string K = "0100" ;
int result = flipCharacters(S, K);
Console.Write(result);
}
}
|
Javascript
<script>
function flipCharacters(b, sub)
{
var res = b.length - b.replace(sub, "" ).length;
var temp = parseInt(res / sub.length);
return temp;
}
var S = "010010" ;
var K = "0100" ;
var result = flipCharacters(S, K);
document.write(result);
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)