Final string after performing given operations | Java 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 Code
class GFG { static String printFinalString ( String s ) { int i , n ; n = s . length ( ) ; int x = 0 , y = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s . charAt ( 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 ; } public static void main ( String args [ ] ) { String s = " xxyyxyy " ; System . out . println ( printFinalString ( s ) ) ; } }