Word Frequency
Medium 40 XPStringsHashingSorting
Commonly asked at Amazon, Zoho
Read a line of lowercase words separated by spaces. Print each distinct word and how many times it appears, as word count, one per line. Sort by count (highest first); break ties alphabetically.
- Input
- One line of words.
- Output
- One line per distinct word.
Sample 1
Input
the cat and the hat and the bat
Output
the 3 and 2 bat 1 cat 1 hat 1
Read input with input() (no prompt message) and print only the answer. Run tries the Input box; Submit checks all test cases.
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.The solution unlocks after 3 submissions.