Minimum steps to remove substring 010 from a binary string Last Updated : 28 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given a binary string, the task is to count the minimum steps to remove substring "010" from this binary string. Examples: Input: binary_string = "0101010" Output: 2 Switching 0 to 1 at index 2 and index 4 will remove the substring 010. Hence the number of steps needed is 2. Input: binary_string = "010" Output: 1 Switching any one 0 to 1 or 1 to 0 will remove the substring 010. Hence the number of steps needed is 1. Approach: Iterate the string from beginning to end-2 of the binary string.If in binary string continuously three characters are '0', '1', '0' then any one character can be change so that one step will be count.Increase the loop counter by 2. Below is the implementation of the above approach: C++ // CPP program to calculate steps // to remove substring 010 // from a binary string #include <bits/stdc++.h> using namespace std; // Function to find the minimum steps int minSteps(string str) { int count = 0; for (int i = 0; i < str.length() - 2; i++) { if (str[i] == '0') { if (str[i + 1] == '1') { if (str[i + 2] == '0') { // substring "010" found count++; i += 2; } } } } return count; } // Driver code int main() { // Get the binary string string str = "0101010"; // Find the minimum steps cout << minSteps(str); return 0; } Java // Java program to calculate steps // to remove substring 010 // from a binary string import java.util.*; class GFG{ // Function to find the minimum steps static int minSteps(String str) { int count = 0; for (int i = 0; i < str.length() - 2; i++) { if (((int)str.charAt(i)) == '0') { if (str.charAt(i + 1) == '1') { if (str.charAt(i + 2) == '0') { // substring "010" found count++; i += 2; } } } } return count; } // Driver code public static void main(String args[]) { // Get the binary string String str = "0101010"; // Find the minimum steps System.out.println(minSteps(str)); } } Python3 # Python3 program to calculate steps # to remove substring 010 # from a binary string # Function to find the minimum steps def minSteps(str): count = 0 i = 0 while i < len(str) - 2: if str[i] == '0': if(str[i + 1] == '1'): if(str[i + 2] == '0'): # substring "010" found count = count + 1 i = i + 2 i = i + 1 return count # Driver code # Get the binary string str = "0101010" # Find the minimum steps print(minSteps(str)) # This code is contributed # by Shashank_Sharma C# // C# program to calculate steps // to remove substring 010 // from a binary string using System; class GFG { // Function to find the minimum steps static int minSteps(string str) { int count = 0; for (int i = 0; i < str.Length - 2; i++) { if (((int)str[i]) == '0') { if (str[i + 1] == '1') { if (str[i + 2] == '0') { // substring "010" found count++; i += 2; } } } } return count; } // Driver code public static void Main() { // Get the binary string string str = "0101010"; // Find the minimum steps Console.Write(minSteps(str)); } } // This code is contributed by ChitraNayal PHP <?php // PHP program to calculate steps to remove // substring 010 from a binary string // Function to find the minimum steps function minSteps($str) { $count = 0; for ($i = 0; $i < strlen($str) - 2; $i++) { if ($str[$i] == '0') { if ($str[$i + 1] == '1') { if ($str[$i + 2] == '0') { // substring "010" found $count++; $i += 2; } } } } return $count; } // Driver code // Get the binary string $str = "0101010"; // Find the minimum steps echo(minSteps($str)); // This code is contributed // by Shivi_Aggarwal ?> JavaScript <script> // js program to calculate steps // to remove substring 010 // from a binary string // Function to find the minimum steps function minSteps(str) { let count = 0; for (let i = 0; i < str.length - 2; i++) { if ((str[i]) == '0') { if (str[i + 1] == '1') { if (str[i + 2] == '0') { // substring "010" found count++; i += 2; } } } } return count; } // Driver code // Get the binary string let str = "0101010"; // Find the minimum steps document.write(minSteps(str)); // This code is contributed by mohit kumar 29. </script> Output2 Time complexity: O(n) where n is the length of binary string Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum steps to remove substring 010 from a binary string S SURENDRA_GANGWAR Follow Improve Article Tags : Strings DSA binary-string substring Practice Tags : Strings Similar Reads Print string after removing all (â10â or â01â) from the binary string Given a binary string str consisting of only 0's and 1's, the task is to print the string after removing the occurrences of "10" and "01" from the string one by one. Print -1 if the string becomes null. Examples: Input: str = "101100" Output: -1 Explanation: In the first step, "10" at index 0 and 1 9 min read 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: 2 Expl 5 min read Minimum substring reversals required to make given Binary String alternating Given a binary string S of length N, the task is to count the minimum number substrings of S that is required to be reversed to make the string S alternating. If it is not possible to make string alternating, then print "-1". Examples: Input: S = "10001110"Output: 2Explanation:In the first operation 7 min read Minimize removal of substring of 0s to remove all occurrences of 0s from a circular Binary String Given circular binary string S of size N, the task is to count the minimum number of consecutive 0s required to be removed such that the string contains only 1s. A circular string is a string whose first and last characters are considered to be adjacent to each other. Examples: Input: S = "11010001" 6 min read Maximum count of â010..â subsequences that can be removed from given Binary String Given a binary string S consisting of size N, the task is to find the maximum number of binary subsequences of the form "010.." of length at least 2 that can be removed from the given string S. Examples: Input: S = "110011010"Output: 3Explanation:Following are the subsequence removed:Operation 1: Ch 6 min read Like