Given a binary string and a number m, the task is to check if the string has m consecutive 1’s or 0’s
Input : str = “001001”, m = 2
Output : YES
Input : str = “1000000001”, m = 10
Output : NO
The approach is to count the consecutive 1’s or 0’s by traversing in the binary string. While traversing in the binary string, keep a count of the number of 1’s or 0’s appearing consecutively. If there are M consecutive 1’s or 0’s, return True else return False.
Given below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the binary string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a binary string contains consecutive same or not
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if the string contains consecutive letters and each letter occurs exactly once
- Check if string is palindrome after removing all consecutive duplicates
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Check three or more consecutive identical characters or numbers
- Python | Check if there are K consecutive 1's in a binary number
- Remove all consecutive duplicates from the string
- Consecutive sequenced numbers in a string
- Remove all consecutive duplicates from a string using STL in C++
- Remove consecutive vowels from string
- Remove three consecutive duplicates from string
- Check if absolute difference of consecutive nodes is 1 in Linked List
- Maximum consecutive repeating character in string
- Largest lexicographical string with at most K consecutive elements
- Group consecutive characters of same type in a string
- Length of longest consecutive ones by at most one swap in a Binary String
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Reduce the string by removing K consecutive identical characters
- Maximum Consecutive Zeroes in Concatenated Binary String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.