EditorialCount the 26 letter frequencies and sort by the tuple (score, length, string). The score is at most 202=40020^2=400202=400, and the total input length is at most 10610^6106. Time is O(20NlogN)O(20N\log N)O(20NlogN). Selection sort suffices for the small subtask.