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.
Input: S = “0111011”, K = “011”
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”
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:
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
- 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 firstname.lastname@example.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.