Stores running count of ' x ' starting from the end
int countX [ ] = new int [ n ] ; int count = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == ' x ' ) count ++ ; countX [ i ] = count ; }