Implement more complete `contain_exactly` algorithm.