Given a binary string str, the task is to check if all the 1’s in the string are equidistant or not. The term equidistant means that the distance between every two adjacent 1’s is same. Note that the string contains at least two 1’s.
Input: str = “00111000”
The distance between all the 1’s is same and is equal to 1.
Input: str = “0101001”
The distance between the 1st and the 2nd 1’s is 2
and the distance between the 2nd and the 3rd 1’s is 3.
Approach: Store the position of all the 1’s in the string in a vector and then check if the difference between each two consecutive positions is same or not.
Below is the implementation of the above approach:
Time Complexity: O(N) where N is the length of the string.
- Python | Check if a given string is binary string or not
- Check if a binary string contains consecutive same or not
- Check divisibility of binary string by 2^k
- Check if a binary string contains all permutations of length k
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if an encoding represents a unique binary string
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Longest sub string of 0's in a binary string which is repeated K times
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be obtained by rotating another string d places
- Check if a string can be obtained by rotating another string 2 places
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.