solution to isValid