Print string after removing all ( “ 10 ” or “ 01 ” ) from the binary string | Java program to print the final String after removing all the occurrences of "10" and "01" from the given binary String ; Function to print the final String after removing all the occurrences of "10" and "01" from the given binary String ; Variables to store the count of 1 ' s ▁ and ▁ 0' s ; Variable left will store whether 0 ' s ▁ or ▁ 1' s is left in the final String ; Length of the String ; For loop to count the occurrences of 1 ' s ▁ and ▁ 0' s in the String ; To check if the count of 1 ' s ▁ is ▁ ▁ greater ▁ than ▁ the ▁ count ▁ of ▁ 0' s or not . If x is greater , then those many 1 's  are printed. ; Length of the final remaining String after removing all the occurrences ; Printing the final String ; Driver Code
import java . util . * ; class GFG { static void finalString ( String str ) { int x = 0 , y = 0 ; int left ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '1' ) x ++ ; else y ++ ; } if ( x > y ) left = 1 ; else left = 0 ; int length = n - 2 * Math . min ( x , y ) ; for ( int i = 0 ; i < length ; i ++ ) { System . out . print ( left ) ; } } public static void main ( String [ ] args ) { String str = "010110100100000" ; finalString ( str ) ; } }