VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-all-the-patterns-of-101-in-a-given-string-general-approach/

⇱ Find all the patterns of "1(0+)1" in a given string (General Approach) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find all the patterns of "1(0+)1" in a given string (General Approach)

Last Updated : 23 Jul, 2025

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 : 2
Recommended Practice

Let size of input string be n. 

  1. Iterate through index '0' to 'n-1'. 
  2. If we encounter a '1', we iterate till the elements are '0'. 
  3. After the stream of zeros ends, we check whether we encounter a '1' or not. 
  4. Keep on doing this till we reach the end of string.

Below is the implementation of the above method. 


Output
2

Time Complexity: O(n) 
Auxiliary Space: O(1)

Comment
Article Tags: