Traverse both strings and check the bits as explained
for ( i = 0 ; i < n ; i ++ ) { if ( s2 . charAt ( i ) == ' ' ) { if ( s1 . charAt ( i ) == ' ' ) { c ++ ; } else { d ++ ; } } else { if ( s1 . charAt ( i ) == ' ' ) { a ++ ; } else { b ++ ; } } }