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.
Input : 1101001 Output : 2 Input : 100001abc101 Output : 2
We have existing solution for this problem please refer Find all the patterns of “1(0+)1” in a given string link. Another set containing similar solution using regex in java is also published.
We will solve this problem quickly in python using Regex. Approach is very simple :
- Search a first sub-string in original string which follows ’10+1′ pattern using re.search(regex,string) method.
- substr = re.search(regex,string) return None if it doesn’t find given regex as sub-string in original string otherwise it returns first matched sub-string which follows ’10+1′ pattern. substr.start() gives us starting index of matched regex and substr.end() gives us ending index of matched regex.
- Whenever we find regex as sub-string then increase count by 1 and again search for given regex starting from ending index of previous sub-string.
- The most occurring number in a string using Regex in python
- Python Regex to extract maximum numeric value from a string
- Python Regex - Program to accept string starting with vowel
- Python Regex | Program to accept string ending with alphanumeric character
- Python regex to find sequences of one upper case letter followed by lower case letters
- Split a String into columns using regex in pandas DataFrame
- Verbose in Python Regex
- Name validation using IGNORECASE in Python Regex
- Pattern matching in Python with Regex
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- Python | Parse a website with regex and urllib
- Python | Swap Name and Date using Group Capturing in Regex
- Regex in Python to put spaces between words starting with capital letters
- Python regex | Check whether the input is Floating point number or not
- Django URL patterns | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.