Find all the patterns of "1(0+)1" in a given string (General Approach) Last Updated : 28 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Try it on GfG Practice A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0's. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. One approach to solve the problem is discussed here, other using Regular expressions is given in Set 2 Examples: Input : 1101001 Output : 2 Input : 100001abc101 Output : 2Recommended Practice1[0]1 Pattern CountTry It! Let size of input string be n. Iterate through index '0' to 'n-1'. If we encounter a '1', we iterate till the elements are '0'. After the stream of zeros ends, we check whether we encounter a '1' or not. Keep on doing this till we reach the end of string. Below is the implementation of the above method. Java // Java Code to count 1(0+)1 // patterns in a string import java.io.*; class GFG { // Function to count patterns static int patternCount(String str) { /* Variable to store the last character*/ char last = str.charAt(0); int i = 1, counter = 0; while (i < str.length()) { /* We found 0 and last character was '1', state change*/ if (str.charAt(i) == '0' && last == '1') { while (i < str.length() && str.charAt(i) == '0') i++; // After the stream of 0's, we // got a '1',counter incremented // Break in case of non zero terminating string. if (i == str.length()) { break; } if (str.charAt(i) == '1') counter++; } if (i == str.length()) { break; } /* Last character stored */ last = str.charAt(i); i++; } return counter; } // Driver Code public static void main (String[] args) { String str = "1001ab010abc01001"; System.out.println(patternCount(str)); } } // This code is contributed by vt_m. C++ /* Code to count 1(0+)1 patterns in a string */ #include <bits/stdc++.h> using namespace std; /* Function to count patterns */ int patternCount(string str) { /* Variable to store the last character*/ char last = str[0]; int i = 1, counter = 0; while (i < str.size()) { /* We found 0 and last character was '1', state change*/ if (str[i] == '0' && last == '1') { while (str[i] == '0') i++; /* After the stream of 0's, we got a '1', counter incremented*/ if (str[i] == '1') counter++; } /* Last character stored */ last = str[i]; i++; } return counter; } /* Driver Code */ int main() { string str = "1001ab010abc01001"; cout << patternCount(str) << endl; return 0; } Python3 # Python3 code to count 1(0+)1 patterns in a # Function to count patterns def patternCount(str): # Variable to store the last character last = str[0] i = 1; counter = 0 while (i < len(str)): # We found 0 and last character was '1', # state change if (str[i] == '0' and last == '1'): while (str[i] == '0'): i += 1 # After the stream of 0's, we got a '1', # counter incremented if (str[i] == '1'): counter += 1 # Last character stored last = str[i] i += 1 return counter # Driver Code str = "1001ab010abc01001" ans = patternCount(str) print (ans) # This code is contributed by saloni1297 C# // C# Code to count 1(0 + )1 // patterns in a string using System; class GFG { // Function to count patterns static int patternCount(String str) { // Variable to store the // last character char last = str[0]; int i = 1, counter = 0; while (i < str.Length) { // We found 0 and last // character was '1', // state change if (str[i] == '0' && last == '1') { while (str[i] == '0') i++; // After the stream of 0's, we // got a '1',counter incremented if (str[i] == '1') counter++; } // Last character stored last = str[i]; i++; } return counter; } // Driver Code public static void Main () { String str = "1001ab010abc01001"; Console.Write(patternCount(str)); } } // This code is contributed by nitin mittal PHP <?php // PHP Code to count 1(0+)1 patterns // in a string // Function to count patterns function patternCount($str) { // Variable to store the // last character $last = $str[0]; $i = 1; $counter = 0; while ($i < strlen($str)) { // We found 0 and last character // was '1', state change if ($str[$i] == '0' && $last == '1') { while ($str[$i] == '0') $i++; // After the stream of 0's, // we got a '1', counter // incremented if ($str[$i] == '1') $counter++; } /* Last character stored */ $last = $str[$i]; $i++; } return $counter; } // Driver Code $str = "1001ab010abc01001"; echo patternCount($str) ; // This code is contributed by nitin mittal ?> JavaScript <script> // javascript Code to count 1(0+)1 // patterns in a string // Function to count patterns function patternCount(str) { /* Variable to store the last character*/ var last = str.charAt(0); var i = 1, counter = 0; while (i < str.length) { /* We found 0 and last character was '1', state change*/ if (str.charAt(i) == '0' && last == '1') { while (str.charAt(i) == '0') i++; // After the stream of 0's, we // got a '1',counter incremented if (str.charAt(i) == '1') counter++; } /* Last character stored */ last = str.charAt(i); i++; } return counter; } // Driver Code var str = "1001ab010abc01001"; document.write(patternCount(str)); // This code is contributed by 29AjayKumar </script> Output2 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Introduction to Pattern Searching - Data Structure and Algorithm Tutorial K kartik Improve Article Tags : Strings Pattern Searching DSA Samsung Practice Tags : SamsungPattern SearchingStrings Similar Reads What is Pattern Searching ? Pattern searching in Data Structures and Algorithms (DSA) is a fundamental concept that involves searching for a specific pattern or sequence of elements within a given data structure. This technique is commonly used in string matching algorithms to find occurrences of a particular pattern within a 5 min read Introduction to Pattern Searching - Data Structure and Algorithm Tutorial Pattern searching is an algorithm that involves searching for patterns such as strings, words, images, etc. We use certain algorithms to do the search process. The complexity of pattern searching varies from algorithm to algorithm. They are very useful when performing a search in a database. The Pat 15+ min read Naive algorithm for Pattern Searching Given text string with length n and a pattern with length m, the task is to prints all occurrences of pattern in text. Note: You may assume that n > m. Examples:Â Input: Â text = "THIS IS A TEST TEXT", pattern = "TEST"Output: Pattern found at index 10 Input: Â text = Â "AABAACAADAABAABA", pattern = 6 min read Rabin-Karp Algorithm for Pattern Searching Given two strings text and pattern string, your task is to find all starting positions where the pattern appears as a substring within the text. The strings will only contain lowercase English alphabets.While reporting the results, use 1-based indexing (i.e., the first character of the text is at po 12 min read KMP Algorithm for Pattern Searching Given two strings txt and pat, the task is to return all indices of occurrences of pat within txt. Examples:Input: txt = "abcab", pat = "ab"Output: [0, 3]Explanation: The string "ab" occurs twice in txt, first occurrence starts from index 0 and second from index 3.Input: txt= "aabaacaadaabaaba", pat 14 min read Z algorithm (Linear time pattern searching Algorithm) This algorithm efficiently locates all instances of a specific pattern within a text in linear time. If the length of the text is "n" and the length of the pattern is "m," then the total time taken is O(m + n), with a linear auxiliary space. It is worth noting that the time and auxiliary space of th 13 min read Finite Automata algorithm for Pattern Searching Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n > m.Examples: Input: txt[] = "THIS IS A TEST TEXT" pat[] = "TEST" Output: Pattern found at index 10 Input: txt[] = "AABAACAADAAB 13 min read Boyer Moore Algorithm for Pattern Searching Pattern searching is an important problem in computer science. When we do search for a string in a notepad/word file, browser, or database, pattern searching algorithms are used to show the search results. A typical problem statement would be-Â " Given a text txt[0..n-1] and a pattern pat[0..m-1] whe 15+ min read Aho-Corasick Algorithm for Pattern Searching Given an input text and an array of k words, arr[], find all occurrences of all words in the input text. Let n be the length of text and m be the total number of characters in all words, i.e. m = length(arr[0]) + length(arr[1]) + ... + length(arr[k-1]). Here k is total numbers of input words. Exampl 15+ min read ÂÂkasaiâs Algorithm for Construction of LCP array from Suffix Array Background Suffix Array : A suffix array is a sorted array of all suffixes of a given string. Let the given string be "banana". 0 banana 5 a1 anana Sort the Suffixes 3 ana2 nana ----------------> 1 anana 3 ana alphabetically 0 banana 4 na 4 na 5 a 2 nanaThe suffix array for "banana" :suffix[] = { 15+ min read Like