Print string after removing all ( “ 10 ” or “ 01 ” ) from the binary string | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void finalString ( string str ) { int x = 0 , y = 0 ; int left ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) x ++ ; else y ++ ; } if ( x > y ) left = 1 ; else left = 0 ; int length = n - 2 * min ( x , y ) ; for ( int i = 0 ; i < length ; i ++ ) { cout << left ; } } int main ( ) { string str = "010110100100000" ; finalString ( str ) ; return 0 ; }