Minimize removal of substring of 0 s to remove all occurrences of 0 s from a circular Binary String | Java program for the above approach ; Function to count minimum number of removal of consecutive 0 s required to make binary string consists only of 1 s ; Stores the count of removals ; Traverse the string S ; If the current character is '0' ; Traverse until consecutive characters are only '0' s ; If the binary string only contains 1 s , then return 1 ; If the first and the last characters are 0 ; Return the resultant count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int minRemovals ( String str , int N ) { int ans = 0 ; boolean X = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '0' ) { ans ++ ; while ( i < N && str . charAt ( i ) == '0' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str . charAt ( 0 ) == '0' && str . charAt ( N - 1 ) == '0' ) { ans -- ; } return ans ; } public static void main ( String [ ] args ) { String S = "11010001" ; int N = S . length ( ) ; System . out . println ( minRemovals ( S , N ) ) ; } }