implement isValid and helper function allDistinct
reorder functions