Take all character in mapT which occur even number of times in respective strings & simultaneously update number of characters left in map
for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS . getOrDefault ( i , 0 ) % 2 == 0 ) { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapS . getOrDefault ( i , 0 ) ) ; mapS . put ( i , 0 ) ; } else { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapS . getOrDefault ( i , 0 ) - 1 ) ; mapS . put ( i , 1 ) ; } if ( mapP . getOrDefault ( i , 0 ) % 2 == 0 ) { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapP . getOrDefault ( i , 0 ) ) ; mapP . put ( i , 0 ) ; } else { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapP . getOrDefault ( i , 0 ) - 1 ) ; mapP . put ( i , 1 ) ; } }