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++
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
bool check(string s, int l)
{
vector< int > pos;
for ( int i = 0; i < l; i++) {
if (s[i] == '1' )
pos.push_back(i);
}
int t = pos.size();
for ( int i = 1; i < t; i++) {
if ((pos[i] - pos[i - 1]) != (pos[1] - pos[0]))
return false ;
}
return true ;
}
int main()
{
string s = "100010001000" ;
int l = s.length();
if (check(s, l))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean check(String s, int l)
{
Vector<Integer> pos = new Vector<Integer>();
for ( int i = 0 ; i < l; i++)
{
if (s.charAt(i)== '1' )
pos.add(i);
}
int t = pos.size();
for ( int i = 1 ; i < t; i++)
{
if ((pos.get(i) - pos.get(i- 1 )) != (pos.get( 1 ) - pos.get( 0 )))
return false ;
}
return true ;
}
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" );
}
}
|
Python3
def check(s, l):
pos = []
for i in range (l):
if (s[i] = = '1' ):
pos.append(i)
t = len (pos)
for i in range ( 1 , t):
if ((pos[i] -
pos[i - 1 ]) ! = (pos[ 1 ] -
pos[ 0 ])):
return False
return True
s = "100010001000"
l = len (s)
if (check(s, l)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool check(String s, int l)
{
List< int > pos = new List< int >();
for ( int i = 0; i < l; i++)
{
if (s[i]== '1' )
{
pos.Add(i);
}
}
int t = pos.Count;
for ( int i = 1; i < t; i++)
{
if ((pos[i] - pos[i - 1]) != (pos[1] - pos[0]))
return false ;
}
return true ;
}
public static void Main(String []args)
{
String s = "100010001000" ;
int l = s.Length;
if (check(s, l))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function check(s, l)
{
var pos = [];
for ( var i = 0; i < l; i++)
{
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 ;
}
var s = "100010001000" ;
var l = s.length;
if (check(s, l))
document.write( "Yes" );
else
document.write( "No" );
</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