Check if all the 1 's in a binary string are equidistant or not | Java implementation of the approach ; Function that returns true if all the 1 's in the binary string s are equidistant ; Initialize vector to store the position of 1 's ; Store the positions of 1 's ; Size of the position vector ; If condition isn 't satisfied ; Drivers code
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 " ) ; } }