A new characterization of alternating and symmetric groups