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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of triplets from the given string with non-equidistant characters
- Python | Check if a given string is binary string or not
- Check divisibility of binary string by 2^k
- Check if a binary string contains consecutive same or not
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- 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 a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if an encoding represents a unique binary string
- Check whether the binary equivalent of a number ends with given string or not
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- String obtained by reversing and complementing a Binary string K times
- 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 formed from another string by at most X circular clockwise shifts
- Check if a string can be converted to another string by replacing vowels and consonants
- Largest sub-string of a binary string divisible by 2
- Print string after removing all (“10” or “01”) from the 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.