26. Letter Frequency Sort

Early summer pressed against the windows of the Dorpat printing house. The iron press laboured steadily, and the air smelled of ink and lilacs drifting in from the yard.

On the compositor’s table lay scattered pages of Estonian verses mixed with Liina’s drafts and stray German and French slips. Karl slammed a drawer of type shut. „We keep running out of the same letters,“ he muttered. „If we’re to print more of Liina’s work, I need to know which ones eat through my stock.“

Johann swung his legs from a crate. „Estonian vowels in the front, German consonants sulking in the back,“ he joked. Liina ignored him, gathering the smudged pages. „They say our language is wild,“ she said. „Show them the letters that carry it, and they might see order instead of noise.“

Her eyes went to Mihkel by the window. „You’ve measured everything else,“ she said. „Can you measure this—just the letters themselves?“ Mihkel leafed through the drafts and felt the familiar pull of pattern. „Give me a sample,“ he said. „I can find which letters appear most often.“

That evening, in his cramped workshop, he stripped one page to its bare letters and fed the sequence into the Logic Mill. The machine scanned and tallied, knowing nothing of meaning, only recurrence. It gathered the unique letters and ranked them from most common to rarest.

When the tape emerged, Mihkel ran his fingers over the ordered symbols. „Even your alphabet has a pulse,“ he thought, imagining Liina’s voice woven through the sounds.

On the input tape, you’ll get a string consisting of a-z letters. Your task is to output each distinct letter exactly once, sorted by how often it appears in the input (from most frequent to least frequent).

The length of the string is between 1 and 50 characters. Among the letters that actually appear in the string, no two different letters have the same frequency.

For example, if the input string is auatau, the output should be aut because a occurs 3 times, u occurs 2 times, and t occurs once.

Sign in to submit your solution.