Count the number of inversions in a string