Minimum number of replacement done of substring "01" with "110" to remove it completely | Java program for the above approach ; Function to find the minimum number of replacement of "01" with "110" s . t . S doesn 't contain substring "10" ; Stores the number of operations performed ; Stores the resultant count of substrings ; Traverse the string S from end ; If the current character is 0 ; If the current character is 1 ; Print the result ; Driver Code
import java . io . * ; class GFG { public static void minimumOperations ( String S , int N ) { int ans = 0 ; int cntOne = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '0' ) { ans += cntOne ; cntOne *= 2 ; } else cntOne ++ ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = "001" ; int N = S . length ( ) ; minimumOperations ( S , N ) ; } }