Check if all the 1’s in a binary string are equidistant or not
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.
Examples:
Input: str = “00111000”
Output: Yes
The distance between all the 1’s is same and is equal to 1.
Input: str = “0101001”
Output: No
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> #include <stdio.h> using namespace std; // Function that returns true if all the 1's // in the binary string s are equidistant bool check(string s, int l) { // Initialize vector to store // the position of 1's vector<int> pos; for (int i = 0; i < l; i++) { // Store the positions of 1's if (s[i] == '1') pos.push_back(i); } // Size of the position vector int t = pos.size(); for (int i = 1; i < t; i++) { // If condition isn't satisfied if ((pos[i] - pos[i - 1]) != (pos[1] - pos[0])) return false; } return true; } // Drivers code int main() { string s = "100010001000"; int l = s.length(); if (check(s, l)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if all the 1's // in the binary string s are equidistant static boolean check(String s, int l) { // Initialize vector to store // the position of 1's Vector<Integer> pos = new Vector<Integer>(); for (int i = 0; i < l; i++) { // Store the positions of 1's if (s.charAt(i)== '1') pos.add(i); } // Size of the position vector int t = pos.size(); for (int i = 1; i < t; i++) { // If condition isn't satisfied if ((pos.get(i) - pos.get(i-1)) != (pos.get(1) - pos.get(0))) return false; } return true; } // Drivers code public static void main(String args[]) { String s = "100010001000"; int l = s.length(); if (check(s, l)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Arnab Kundu |
Python3
# Python3 implementation of the approach # Function that returns true if all the 1's # in the binary s are equidistant def check(s, l): # Initialize vector to store # the position of 1's pos = [] for i in range(l): # Store the positions of 1's if (s[i] == '1'): pos.append(i) # Size of the position vector t = len(pos) for i in range(1, t): # If condition isn't satisfied if ((pos[i] - pos[i - 1]) != (pos[1] - pos[0])): return False return True # Driver code s = "100010001000"l = len(s) if (check(s, l)): print("Yes") else: print("No") # This code is contributed # by mohit kumar |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Function that returns true if all the 1's // in the binary string s are equidistant static bool check(String s, int l) { // Initialize vector to store // the position of 1's List<int> pos = new List<int>(); for (int i = 0; i < l; i++) { // Store the positions of 1's if (s[i]== '1') { pos.Add(i); } } // Size of the position vector int t = pos.Count; for (int i = 1; i < t; i++) { // If condition isn't satisfied if ((pos[i] - pos[i - 1]) != (pos[1] - pos[0])) return false; } return true; } // Drivers code public static void Main(String []args) { String s = "100010001000"; int l = s.Length; if (check(s, l)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } /* This code contributed by PrinciRaj1992 */ |
Yes
Time Complexity: O(N) where N is the length of the string.
Recommended Posts:
- 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 has two consecutive occurrences of one everywhere
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if a binary string contains all permutations of length k
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- 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
- Queries to check if string B exists as substring in string A
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


