Frequency of single character can be possibly changed , so change its parity
for ( int j = 0 ; j < 26 ; j ++ ) { List < Integer > changedCount = new ArrayList < > ( ) ; changedCount . addAll ( a ) ; if ( a . get ( j ) == 0 ) changedCount . set ( j , 1 ) ; else changedCount . set ( j , 0 ) ; ans += mp . getOrDefault ( changedCount , 0 ) ; } mp . put ( a , mp . getOrDefault ( a , 0 ) + 1 ) ; } return ans ; }