Minimum number of steps needed to remove the substring K from given string

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output: 

1

 

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

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : nidhi_biet, rutvik_56