Greedy algorithm based on =/=s working correctly