The most important observation here is that similar characters must be together. I want to ask people who solved it by themselves reached to that observation ? It was asked by codebuster_10 here but no one replied. Editorial gives the mathematical proof but I want to know the lane of thought (because first we make guess and then justify).
Also it will be good if someone provides easier way of justifying it.