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 has two consecutive occurrences of one everywhere
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Check if a string has all characters with same frequency with one variation allowed
- Efficiently check if a string has all unique characters without using any additional data structure
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Construct a string that has exactly K subsequences from given string
- Check if the string contains consecutive letters and each letter occurs exactly once
- Check if a binary string contains consecutive same or not
- Check if string is palindrome after removing all consecutive duplicates
- Determine if a string has all Unique Characters
- Print the string after the specified character has occurred given no. of times
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Transform a string such that it has abcd..z as a subsequence
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Check if absolute difference of consecutive nodes is 1 in Linked List
- Python | Check if there are K consecutive 1's in a binary number
- Check if all bits can be made same by flipping two consecutive bits
- Check three or more consecutive identical characters or numbers
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.