Imagine you're a text analyst tasked with understanding the most common words in a document. Your job is to create a bash script that counts how many times each word appears in a text file called words.txt.
The output should display each unique word followed by its frequency count, sorted by frequency in descending order. For words with the same frequency, sort them alphabetically.
Example: If words.txt contains:the quick brown fox jumps over the lazy dog the fox
Your script should output:the 3
fox 2
brown 1
dog 1
jumps 1
lazy 1
over 1
quick 1
Constraints:
- Input contains only lowercase letters and spaces
- Words are separated by one or more whitespace characters
- Output format:
word frequency(space-separated)
Input & Output
Visualization
Time & Space Complexity
O(n) to read and count words, plus O(k log k) to sort k unique words. Overall linear in input size.
Space for associative array storing k unique words and their counts
Constraints
- Input file contains only lowercase letters (a-z) and space characters
- Words are separated by one or more whitespace characters
- File size can be up to 106 characters
- Output format: Each line should contain 'word count' separated by a single space
- Words with same frequency should be sorted alphabetically