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:2Explanation:

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:1Explanation:

In the above string we have the substring 0100 for 1 time, change the 4th bit of the string to 1.

**Naive Approach:** The naove 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:

## 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);` ` ` `}` `}` |

*chevron_right*

*filter_none*

## Python

`# Python 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)` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** *O(N)*, where N is the length of the string. **Auxiliary Space:** *O(1)*

## Recommended Posts:

- Minimum steps to remove substring 010 from a binary string
- Minimum steps to delete a string by deleting substring comprising of same characters
- Minimum number of Appends needed to make a string palindrome
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Minimum characters to be replaced to remove the given substring
- Minimum length of substring whose rotation generates a palindromic substring
- Minimum number of bracket reversals needed to make an expression balanced
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of letters needed to make a total of n
- Find minimum number of steps to reach the end of String
- Minimum changes required to make first string substring of second string
- Minimum amount of lamps needed to be installed
- Minimum minutes needed to make the time palindromic
- Minimum size substring to be removed to make a given string palindromic
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Minimum steps to convert one binary string to other only using negation
- Check if the given string is shuffled substring of another string
- Check if given string is a substring of string formed by repeated concatenation of z to a

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.