Final string after performing given operations | C ++ implementation of the approach ; Function to return the modified string ; Count number of ' x ' ; Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string printFinalString ( string s ) { int i , n ; n = s . length ( ) ; int x = 0 , y = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' x ' ) x ++ ; else y ++ ; } string finalString = " " ; if ( x > y ) for ( i = 0 ; i < x - y ; i ++ ) finalString += " x " ; else for ( i = 0 ; i < y - x ; i ++ ) finalString += " y " ; return finalString ; } int main ( ) { string s = " xxyyxyy " ; cout << printFinalString ( s ) ; }