Check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of a Binary String | Function to check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of string S ; Stores the indices of all 1 s ; Store the number of pairs separated by at least M 0 s ; Traverse the string ; Store the current index ; Traverse the array containing indices ; If the number of 0 s > M , then increment cnt by 1 ; Increment cnt ; Check if at least M '0' s lie between the first and last occurrence of '1 ; Increment cnt ; If the value of cnt <= 1 , then rotation of string is possible ; Otherwise ; Driver Code
def rotateString ( n , m , s ) : NEW_LINE INDENT v = [ ] NEW_LINE cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT DEDENT for i in range ( 1 , len ( v ) ) : NEW_LINE INDENT if ( ( v [ i ] - v [ i - 1 ] - 1 ) > m ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( len ( v ) >= 2 and ( n - ( v [ - 1 ] - v [ 0 ] ) - 1 ) > m ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( cnt <= 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "101001" NEW_LINE M = 1 NEW_LINE N = len ( S ) NEW_LINE rotateString ( N , M , S ) NEW_LINE DEDENT