Note. Contains numbers and lowercase letters only. The string is not necessarily binary. 100201 is not a valid template.
Input: 1101001 Output: 2 Input: 100001abc101 Output: 2
We have a solution to this problems, please refer to link. Also posted another set containing a similar solution Regex . The approach is very simple:
- Find the first substring in the original string that matches the pattern "10 +1" using method re.search (regex, string) .
- substr = re.search (regex, string) returns None, if it does not find the given regexp as a substring in the original string, otherwise it returns the first matched substring that follows the pattern "10 +1". substr.start () gives us the starting index of the matching regular expression, and substr.end () gives us the ending index of the matching regular expression.
- Whenever when we find a regular expression as a substring, increment the counter by 1 and look for the given regular expression again, starting at the ending index of the previous substring.