A podcast producer has an automatic transcript of an episode and wants a quick tally of which words the host used most. Given the full transcript text, split it into words wherever one or more whitespace characters (spaces, tabs, or newlines) occur, and count how many times each distinct word occurs. Word matching is case-sensitive, so "Sound" and "sound" are counted as two different words.
Report every distinct word together with its count, ordered from the most frequent word to the least frequent. If two or more words occur exactly the same number of times, break the tie by ordering those words in ascending ASCII (lexicographic) order.
The entire input is the transcript text, given over one or more lines. Read all of standard input; the line breaks within the transcript carry no special meaning beyond separating words.
Print one line per distinct word, in the order described above, as the word followed by a single space followed by its integer count.
Example 1
Input
the day is sunny the the the sunny is is
Expected
the 4 is 3 sunny 2 day 1
Explanation
Splitting on whitespace gives the tokens: the, day, is, sunny, the, the, the, sunny, is, is. Counting: 'the' appears 4 times, 'is' appears 3 times, 'sunny' appears 2 times, 'day' appears 1 time. Sorted by descending count (no ties here), the output lists 'the 4', 'is 3', 'sunny 2', 'day 1'.
Example 2
Input
red blue green red blue
Expected
blue 2 red 2 green 1
Explanation
Tokens: red, blue, green, red, blue. Counts: red=2, blue=2, green=1. 'red' and 'blue' tie at count 2, so the tie is broken alphabetically: 'blue' comes before 'red'. Output: 'blue 2', 'red 2', 'green 1'.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →