Minimize removal of substring of 0 s to remove all occurrences of 0 s from a circular Binary String | Function to count minimum number of removal of consecutive 0 s required to make binary consists only of 1 s ; Stores the count of removals ; Traverse the S ; If the current character is '0' ; Traverse until consecutive characters are only '0' s ; If the binary only contains 1 s , then return 1 ; If the first and the last characters are 0 ; Return the resultant count ; Driver Code
function minRemovals ( str , N ) { let ans = 0 ; let X = false ; for ( i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { ans ++ ; while ( str [ i ] == ' ' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str [ 0 ] == ' ' && str [ N - 1 ] == ' ' ) { ans -- ; } return ans ; } let S = " " ; let N = S . length ; document . write ( minRemovals ( S , N ) ) ;