Check if all the 1's in a binary string are equidistant or not
Last Updated :
07 Jul, 2022
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 */
JavaScript
<script>
// Javascript implementation of the approach
// Function that returns true if all the 1's
// in the binary string s are equidistant
function check(s, l)
{
// Initialize vector to store
// the position of 1's
var pos = [];
for(var i = 0; i < l; i++)
{
// Store the positions of 1's
if (s[i] == '1')
pos.push(i);
}
// Size of the position vector
var t = pos.length;
for(var 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
var s = "100010001000";
var l = s.length;
if (check(s, l))
document.write( "Yes");
else
document.write("No");
// This code is contributed by noob2000
</script>
Time Complexity: O(N) where N is the length of the string.
Space Complexity: O(N) where N is for the extra position vector
Similar Reads
Check if a Binary String contains A pairs of 0s and B independent 0s or not Given a binary string S and two positive integers A and B, the task is to check if the string consists of A independent pair of adjacent 0s and B independent number of 0s in the binary string or not. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: S = "10100", A = 1, B
9 min read
Check if a string has m consecutive 1's or 0's Given a binary string and a number m, the task is to check if the string has m consecutive 1's or 0's. Examples: 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 the binary string. While traver
5 min read
Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1s Given two binary strings A and B of length N, the task is to check if the string A can be converted to B by reversing substrings of A which contains even number of 1s. Examples: Input: A = "10011", B = "11100"Output: YesExplanation: Reverse substring A[2, 5], 10011 â 11100.After completing the above
9 min read
Check if a Binary String can be sorted in decreasing order by removing non-adjacent characters Given a binary string S of size N, the task is to check if the binary string S can be sorted in decreasing order by removing any number of the non-adjacent characters. If it is possible to sort the string in decreasing order, then print "Yes". Otherwise, print "No". Examples: Input: S = â10101011011
7 min read
Check if all occurrences of a character appear together Given a string s and a character c, find if all occurrences of c appear together in s or not. If the character c does not appear in the string at all, the answer is true. Examples Input: s = "1110000323", c = '1' Output: Yes All occurrences of '1' appear together in "1110000323" Input: s = "3231131"
7 min read