Minimum number of steps needed to remove the substring K from given string
Last Updated :
15 Jul, 2025
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++
// C++ program for the above approach
#include<bits/stdc++.h>
#include <boost/algorithm/string/replace.hpp>
using namespace std;
// Function that counts the total
// number of character to be flipped
// such the string b doesn't contains
// string sub as a substring
int flipCharacters(string b,
string sub)
{
// Replacing the substring with ""
// and find the difference between
// the length of the resultant
// string and the original
// string length
int res = b.size();
boost::replace_all(b, sub, "");
res = res - b.size();
// Divide the result
// with substring length
int temp = res / sub.size();
// Return the final count
return temp;
}
// Driver Code
int main()
{
// Given string S and substring K
string S = "010010";
string K = "0100";
// Function call
int result = flipCharacters(S, K);
// Print the minimum flip
cout << (result);
}
// This code is contributed by Amit Katiyar
Java
// Java program for the above approach
import java.util.*;
public class Test {
// Function that counts the total
// number of character to be flipped
// such the string b doesn't contains
// string sub as a substring
static int flipCharacters(String b,
String sub)
{
// Replacing the substring with ""
// and find the difference between
// the length of the resultant
// string and the original
// string length
int res = b.length()
- b.replaceAll(sub, "")
.length();
// Divide the result
// with substring length
int temp = res / sub.length();
// Return the final count
return temp;
}
// Driver Code
public static void main(String[] args)
{
// Given string S and substring K
String S = "010010";
String K = "0100";
// Function Call
int result = flipCharacters(S, K);
// Print the minimum flip
System.out.println(result);
}
}
Python3
# Python3 program for the above approach
def flipCharacters(b, sub):
# Replace the substring with
# "" (emptryString)
b1 = b.replace(sub, "")
n = int((len(b)-len(b1))/len(sub))
return n
# Driver Code
if __name__ == '__main__':
# Given string S and substring K
S ="010010"
X ="0100"
# Function Call
result = flipCharacters(S, X)
# Print the minimum flip
print(result)
C#
// C# program for the above approach
using System;
class GFG
{
// Function that counts the total
// number of character to be flipped
// such the string b doesn't contains
// string sub as a substring
static int flipCharacters(string b,
string sub)
{
// Replacing the substring with ""
// and find the difference between
// the length of the resultant
// string and the original
// string length
int res = b.Length -
b.Replace(sub, "").Length;
// Divide the result
// with substring length
int temp = res / sub.Length;
// Return the final count
return temp;
}
// Driver Code
public static void Main(string[] args)
{
// Given string S and substring K
string S = "010010";
string K = "0100";
// Function Call
int result = flipCharacters(S, K);
// Print the minimum flip
Console.Write(result);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// JavaScript program for the above approach
// Function that counts the total
// number of character to be flipped
// such the string b doesn't contains
// string sub as a substring
function flipCharacters(b, sub)
{
// Replacing the substring with ""
// and find the difference between
// the length of the resultant
// string and the original
// string length
var res = b.length - b.replace(sub, "").length;
// Divide the result
// with substring length
var temp = parseInt(res / sub.length);
// Return the final count
return temp;
}
// Driver Code
// Given string S and substring K
var S = "010010";
var K = "0100";
// Function call
var result = flipCharacters(S, K);
// Print the minimum flip
document.write(result);
</script>
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)