Techniques to Find Consecutive 1s or 0s in a Python String
Last Updated : 23 May, 2024
We are given a binary string and a number m, and we have to check if the string has m consecutive 1’s or 0’s. Below are a few examples to understand the problem statement clearly.
Examples:
Input: str = “001001”, m = 2
Output: True
Explanation: the string have 2 consecutive 0s
Input: str = “1000000001”, m = 10
Output: False
Explanation: the string neither has 10 consecutive 0s or 1s
Check If a Binary String has m Consecutive
Now let us see a few different approaches to check if a binary string has m consecutive 1s or 0s in Python.
Brute Force Approach
In this method, we will check each character in the string one by one. Two counter variables are taken to track the count of 0s and 1s. Then iterating through each character using for loop, the counter variables are updated. For each character, the value of its counter is increased by one while the other's is reset to 0. Then check if any counter has reached the value m, return True.
Example: In this example, the counter variables are initialized to 0 and then using for loop each character is extracted from the string and its corresponding counter is increased while the other is reset. Then each counter is checked if it matched the value of m or not.
Python def has_consecutive(s, m): # Initialize counters for consecutive 1s and 0s count_1 = count_0 = 0 # Loop through each character in the string for char in s: # Update counters if char == '1': count_1 += 1 count_0 = 0 elif char == '0': count_0 += 1 count_1 = 0 # Check if any counter has reached m if count_1 >= m or count_0 >= m: return True return False s = "11110000111111" m = 4 print(has_consecutive(s, m))
Output:
True
Using String Methods
In this method, we are using the Python string property where when a string is multiplied with an integer, it returns the integer-times string. That is, we we write 5*'a', it will give us 'aaaaa'. Using this technique, we will define two target strings, for 0 and 1. Then we will check for the target string in the given string. If the target string is found in the given string, return True.
Example: In this example, two target strings are initialized by multiplying 0 and 1 with the m. Then these target string are checked for in the given string using Python membership operator - in.
Python def has_consecutive(s, m): # Create target strings with m consecutive '1's and '0's target_1 = '1' * m target_0 = '0' * m # Check if either target string is a substring of s return target_1 in s or target_0 in s s = "11110001" m = 4 print(has_consecutive(s, m))
Output:
True
Using Regular Expression
This method is quite similar to the previous one. In this method we are using Python Regular Expression module to check for a specific pattern. The search() function of this module is used to search for a specific patter. It takes two arguments, the first is the pattern to be searched, and second is the string in which the pattern is to be searched.
Example: In this example, we first import the regular expression module. Then define the target pattern using string multiplication technique. Then the search() function checks if the target string is in the original string or not.
Python import re def has_consecutive(s, m): # Create target strings with m consecutive '1's and '0's target_1 = '1' * m target_0 = '0' * m # Check if either target string is a substring of s if re.search(target_1, s) or re.search(target_0, s): return True return False s = "1001010" m = 3 print(has_consecutive(s, m))
Output:
False