Store the indices which contains the vowels
Vector < Integer > indices = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' a ' || s . charAt ( i ) == ' e ' || s . charAt ( i ) == ' o ' || s . charAt ( i ) == ' i ' || s . charAt ( i ) == ' u ' ) { if ( counts . containsKey ( s . charAt ( i ) ) ) { counts . replace ( s . charAt ( i ) , counts . get ( s . charAt ( i ) ) + 1 ) ; } else { counts . put ( s . charAt ( i ) , 1 ) ; } indices . add ( i ) ; } }